-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Constructors for common base types? #34
Comments
Some more:
|
These are from running the Adapt.jl test suite, https://github.com/JuliaGPU/Adapt.jl/blob/master/test/runtests.jl, with the following test macro: julia> macro test_adapt(to, src, dst, typ=nothing)
quote
@test Flatten.modify(CustomArray, $src, Array) == $dst
@test typeof(Flatten.modify(CustomArray, $src, Array)) == typeof($dst)
if $typ !== nothing
@test typeof($dst) <: $typ
end
end
end |
As a general rule, I think if a type satisfies the following:
|
Yeah its an edge case I guess, but mostly because the constructor is But it's the most widely used, and not defining it means other packages might have to pirate |
Maybe of interest to the gpu use case: jw3126/Setfield.jl#149 |
Most of that discussion is solved with |
Ok, it may be pragmatic to support |
This is kind of complicated in practise, and brings up some questions for field setting in gerneral. In https://github.com/JuliaLang/julia/blob/master/base/subarray.jl#L37 So setting them without changing struct SubArrayConstructor end
constructorof(::Type{<:SubArray}) = SubArrayConstructor()
(::SubArrayConstructor)(parent, indices, args...) = view(parent, indices) Which just throws out But we could clearly document that I guess - base fields that are always precomputed cannot be modified manually. |
Yes it is not ideal. But I think we could add the general rule that constructorof(obj)(fieldvalues(obj, public=true)...) == obj and fieldvalues(subarray, public=true) == (subarray.parent, subarray.indices) |
Another interesting finding... I'm not sure how common that is in base, but I hope it's rare... that's so awful to work with. |
Tim Besard just made the point
And of course
Should we add some constructors for common base wrapper types like
SubArray
?The text was updated successfully, but these errors were encountered: