syntactic sugar Foo{<:Bar} for Foo{T} where T<:Bar#20414
syntactic sugar Foo{<:Bar} for Foo{T} where T<:Bar#20414JeffBezanson merged 6 commits intoJuliaLang:masterfrom
Conversation
| ``` | ||
|
|
||
| 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.
Worth linking to https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science), do you think?
|
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
wheresyntax, e.g.TwoParams{S,<:Number} where S<:Realworks.If you nest types, then the
wheregoes 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.