-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add wrapper for passing constants through type inference #9452
Conversation
+1 from one of the users of |
+1! |
#9196 served as a trial balloon for using a special character for this ( |
+1 |
Whoops... forgot I already +1'd this... Anyway, I like |
Add wrapper for passing constants through type inference
OK, now set in stone :-). |
|
cute :-) |
Should we add any docs about this? I think I missed a larger discussion somewhere about what this does, so I feel a little in the dark. How can one use this? |
For one example, you can use it to dispatch on Bool values. |
I'm preparing a pull request where I use them to construct triangular matrices, e.g. |
See #9540. @andreasnoack, note that in the documentation I created instances rather than types in function calls. We should probably pick one or the other and use it consistently. I chose this way largely because Jeff has expressed this as his (mild) preference. |
In the cases where I'm considering |
I confess I largely agree with you---using types decreases the burden at the call site. This is something that perhaps others should chime in on; if we're not consistent, it will be frustrating for users. |
I strongly prefer using types rather than instances for this entire pattern, above and beyond the specific case of |
Is there a performance reason to choose one over the other? |
I'm not really sure, but I don't believe so. I think @JeffBezanson has reasons for being unhappy with the "types as tags" patterns, perhaps related to the accumulation of an excessive number of different methods. But it seems to me like we're already doing things with the type system that weren't quite intended and that |
What about defining |
@rfourquet The reason for introducing |
A syntactic sugar related question: what do you all think about using this macro for symbol-valued macro §_str(s)
isa(s, AbstractString) ||
error("@§_str only accepts a literal string")
Expr( :curly, :Val, QuoteNode( symbol( s ) ) )
end so Pros:
a = :L
Triangular( A, Val{a} ) which defeats the whole purpose (at least a significant part of the purpose, I think).
Cons:
(note: § is not a valid label character in julia, but you can type it in REPL using \S[tab]) |
I tried with |
@andreasnoack oops, thanks for pointing out. |
I wonder if this all points to needing a nice, Julian implementation of enums that would serve these purposes. Enums could serve as the official, "dispatchable" values/instances. They could also be useful for implementing Tim Holy's traits as well along with the cases being discussed here. |
Just as Tim said, it's a mild preference. I won't object to |
OK, let's go with |
Thanks for chiming in, Jeff! |
Coming a bit late to the party, but type instability of It's nicer to type |
@toivoh, that doesn't work: julia> import Base.Val
julia> @inline Val(x) = Val{x}()
Val{T}
julia> arraytype{N}(::Type{Val{N}}) = Array{Float32,N}
arraytype (generic function with 1 method)
julia> arraytype{N}(::Val{N}) = Array{Float32,N}
arraytype (generic function with 2 methods)
julia> f() = arraytype(Val(3))
f (generic function with 1 method)
julia> g() = arraytype(Val{3})
g (generic function with 1 method)
julia> Base.return_types(f, ())
1-element Array{Any,1}:
Type{_<:Array{Float32,N}}
julia> Base.return_types(g, ())
1-element Array{Any,1}:
Type{Array{Float32,3}} |
Aha. Thanks for checking! |
Ah yes thanks for checking, I wanted to but it wasn't clear to me how to do it. |
I presume this basically comes down to a difference between expression-interpolation and inlining. I suspect this fails at the first step, on type inference of It does seem possible that this could be "fixed" (with the risk, of course, that it would break other stuff and not be acceptable). If you're interested in pursuing this, if I were you I'd look into the order of events in
I don't know inference.jl well enough to be confident of this, unfortunately, and don't have time to look into it further. But if you want this changed, it would be best if you or someone looked into this before |
Am I correct in saying that The consequence is that callers using It seems to me that |
Also we can define for instance |
See #22475 for the suggested changes. |
It occurred to me that this doesn't have its own PR yet. This is so trivial, let's try to bikeshed the name fairly quickly (we've all had some months to think about this).