-
-
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
syntactic sugar Foo{<:Bar} for Foo{T} where T<:Bar #20414
Conversation
@@ -711,6 +714,17 @@ julia> Pointy{Real} <: Pointy{Float64} | |||
false | |||
``` | |||
|
|||
The notation `Pointy{<:Real}` can be used to express the Julia analogue of a | |||
*covariant* type, while `Pointy{>:Int}` the analogue of a *contravariant* type, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth linking to https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science), do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a link above.
I have to admit, this is really nice. As it happens, it's also gentler on the type/dispatch system, since it moves |
Since this syntax parses in Julia 0.5, it should be possible to make The nested case, e.g. |
Yes, the nested case can't be expressed in 0.5. The closest thing might be to have Compat convert |
Oh, cool, I didn't know about julia> @eval f(::Array{$(TypeVar(:T, Real, false))}) = 1
f (generic function with 1 method)
julia> f([2,3,4])
1 |
Should be good to merge? |
Can you do ex |
@KristofferC, no, if you want to do that I think you need to declare a type parameter explicitly. Otherwise, what is the scope for Note that if you do |
Ah, thanks for explaining. |
At some point we should take a pass through Base and use this where possible; that will help eliminate uses of to-be-deprecated static parameter syntax. |
Unfortunately, it looks like the |
(Of course, I could just |
For most (all?) cases, simply leave a |
@yuyichao, great, I thought the |
This closes #6984 by adding the syntactic sugar
Foo{<:Bar}
forFoo{T} where T<:Bar
. Update: similarly forFoo{>:Bar}
.It combines fine with the
where
syntax, e.g.TwoParams{S,<:Number} where S<:Real
works.If you nest types, then the
where
goes with the innermost type where the<:
appeared, e.g.Vector{Foo{<:Bar}}
isVector{Foo{T} where T<:Bar}
. You can nest these, e.g.Vector{<:Foo{<:Bar}}
works.