-
Notifications
You must be signed in to change notification settings - Fork 148
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
Inferrably construct leaf types #181
Conversation
I don't think there's a way to do this currently, and what you've got here looks like a great solution to me. Is it fair to say that this is mainly useful because writing the concrete type for |
The one interesting fact about this PR's implementation is that we call a type and don't get an instance of that type (it's not a constructor). Does this happen elsewhere in the language? E.g. we use OTOH it we already have: julia> similar_type(SArray, Float64, Size(2,2))
StaticArrays.SMatrix{2,2,Float64,4} and users can easily overload the |
By the way, I think |
Ok, good point :-/
|
Yes, that's precisely the issue.
There would be a certain sense in being able to write this as
I knew there had to be a method for "spelling" this, I should have reread the README... But I do think that when one wants a specific type, it would be better to be able to spell that out explicitly. Maybe one should implement this as
It just doesn't tend to come up, perhaps because there aren't that many arrays with dependent type parameters. Or I build a single instance and then use |
This is inferrable though: julia> Base.@pure pure_mul(a::Int, b::Int) = a * b
pure_mul (generic function with 1 method)
julia> foo{M,N}(::NTuple{M}, ::NTuple{N}) = NTuple{pure_mul(M,N), Any}
foo (generic function with 1 method)
julia> @code_warntype foo((1,2), (3,4,5))
Variables:
#self#::#foo
#unused#@_2::Tuple{Int64,Int64}
#unused#@_3::Tuple{Int64,Int64,Int64}
Body:
begin
return NTuple{6,Any}
end::Type{NTuple{6,Any}} |
Indeed it is. I was just noting that it's an extra step that would be nice to not have to think about. |
I wonder if some functions in Base could be special cased to be considered pure.. Like simple arithmetic on some leaf types (like |
There's also the "do more constant propagation during inference" approach (e.g., JuliaLang/julia#5560), but my understanding is that this is not a finite problem. |
I can close this and use |
Does julia> similar_type(MVector, Int, Size(2,))
SVector{2,Int64} # Now it's immutable
I quite like what's here, with the exception that the |
In my book, if it's OK in Like Chris said, here What we kind-of want is to overload (Is "leaf" or "concrete" the usual term from the user side? I've always seen "visualize the type-tree" terms like leaf, bottom, top, etc as being more used internally by Julia devs, while the docs mainly distinguish abstract vs. concrete) |
Ok @andyferris if you think it's a bad pun, I think we should go with The large problem is that we've complicated the semantics of |
By the way, in the future I could imagine having a somewhat more flexible
instead of
(of course, that's speculation at the moment.) |
Tim, would If |
It does for my immediate needs. We clearly need a better way to do this; https://github.com/vtjnash/ComputedFieldTypes.jl seems like a pretty good answer, though. It would be a big job to switch, so I'd be curious to know if it's of interest to you folks. (I might put together a PR, but not in the near future.) |
Interesting, thanks for the link. |
Hmm, don't rush to adopt ComputedFieldTypes just yet: vtjnash/ComputedFieldTypes.jl#2. |
I just transitioned a large internal project from FixedSizeArrays->StaticArrays. I may have missed it, but the one bit of functionality I found lacking was the ability to easily & inferrably create a leaftype. I use this facility to allocate
Array{SA}
whereSA <: StaticArray
gets constructed from other type parameters. Theleaf2
test illustrates how you might inferrably create an empty vector of NxN SMatrix with eltype Float32, given any function argument that gives you access toN
as a type parameter:It's just one line of code, but documenting and testing it seems to be the main thing here.
Again, maybe there's already a different way to do this, so if I missed it please do let me know.