-
-
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
fully switch static parameter syntax to where
#11310
Comments
It is actually not limited to when the parameter is not used. The following script prints A{Int64}(1)
Int64 type A{T}
a
end
A{T}(::T) = T
type B{T}
a
end
call{T}(::Type{B{T}}, ::T) = T
println(A{Int}(1))
println(B{Int}(1)) |
And I guess the issue is that (in the above case), whether IMO, the current behavior is better. It might be possible to make the non ambigious case working but it will be easily broken if you add other parameters/arguments to the definition. |
Or maybe we can allow sth like |
Ok, so I guess the original example is effectively lowered to It's a shame, because Julia almost always achieves an amazing level of Just Makes Sense™, but I always find myself banging my head against parametric type constructors. |
(I also find parametric type constructors one of the more confusing parts of the language, especially if there is a mix of inner and outer constructors) |
I just end up only using overloading of |
This is the best idea I currently have for dealing with this: |
In @one-more-minute's original example, it is a bit easier to see what is going on when using different symbols for the parameters: julia> type Foo{TT}
bar
end
julia> Foo{A}() = Foo{A}(1)
WARNING: static parameter A does not occur in signature for call at none:1.
The method will not be callable.
Foo{TT}
julia> methods(call, (Type{Foo},))
2-element Array{Any,1}:
call{A}(::Type{Foo{TT}}) at none:1
call{T}(::Type{T}, args...) at essentials.jl:57 (Aside: resolving #10794 would make the display clearer in the original example.) Instead the expectation was that the following call method would be generated: julia> call{A}(::Type{Foo{A}}) = Foo{A}(1) (I'm sure this is clear to most but wasn't to me.) |
I was thinking some more about this syntax problem today. We need to fix this and obtain syntax for "unionall" types at the same time, since underneath they're the same issue. I thought of a few principles I think a solution should obey:
One syntax that obeys these is to use some infix operator, here
Using dot is marginal, but might be possible since dot surrounded by spaces is currently deprecated. Some other options are infix
It's as if you're adding definitions for every |
I kind of like the |
In the line
the text Alternatively, using a "bigger" visual separator before the function name might help. What about Maybe the
|
Ideally this will be new syntax, so we can use it for free-standing unionall types as well without ambiguity. So
|
I think |
One could also imagine using |
I second that, |
I would love forall and having ∀ as a synonym, which wouldn't extend long function definition lines much further. |
I'm not sure |
|
Where only reads right if it comes after. On Monday, January 11, 2016, Eric Davies notifications@github.com wrote:
|
Just throwing another idea. Maybe function with T<:Number +(x::T, y::T) #= function body =# end
with T<:Number +(x::T, y::T) = #= function body =# |
+1 for |
|
Maybe, besides of using |
Having read through this again, I think the qualifier should come after (as was discussed before over in #13412 (comment)): function +(x::T, y::T) with T<:Number
....
end
+(x::T, y::T) with T<:Number = ...
const IntVector = Array{T, 1} with T<:Integer versus function with T<:Number +(x::T, y::T)
...
end
with T<:Number +(x::T, y::T) = ...
const IntVector = with T<:Integer Array{T, 1} (you can replace The reason being that I'm interested most in the function name and not some type-qualifiers, the same goes for the "uinonall" types. Thus it should come first (and I think this is more important than consistency with ## Current syntax (abstractarraymath.jl)
conj{T<:Real}(x::AbstractArray{T}) = x
conj!{T<:Real}(x::AbstractArray{T}) = x
real{T<:Real}(x::AbstractArray{T}) = x
imag{T<:Real}(x::AbstractArray{T}) = zero(x)
+{T<:Number}(x::AbstractArray{T}) = x
*{T<:Number}(x::AbstractArray{T,2}) = x
## Syntax with following qualifier
conj(x::AbstractArray{T}) with T<:Real = x
conj!(x::AbstractArray{T}) with T<:Real = x
real(x::AbstractArray{T}) with T<:Real= x
imag(x::AbstractArray{T}) with T<:Real = zero(x)
+(x::AbstractArray{T}) with T<:Number= x
*(x::AbstractArray{T,2}) with T<:Number = x
## Syntax with preceding qualifier
with T<:Real conj(x::AbstractArray{T}) = x
with T<:Real conj!(x::AbstractArray{T}) = x
with T<:Real real(x::AbstractArray{T}) = x
with T<:Real imag(x::AbstractArray{T}) = zero(x)
with T<:Number +(x::AbstractArray{T}) = x
with T<:Number *(x::AbstractArray{T,2}) = x |
+1 Ok, I agree with the "one-line definitions" argument. |
@mauro3 That's pretty convincing. It also lets us use
which doesn't look too bad. |
+1 for the postfix would for clarity, i'm hoping we can have newlines:
|
I think that's some pretty difficult lookahead for the parser. I believe there is no situation in the present syntax where a complete expression followed by a newline can be augmented by something on the next line. Seems more doable if you let the keyword dangle:
|
switch more code to `where` syntax. part of #11310.
switch remaining files to `where` syntax (#11310)
…uliaLang#11310 (JuliaLang#22834)" This partially reverts commit c6e51d0.
…uliaLang#11310 (JuliaLang#22834)" This partially reverts commit c6e51d0.
…uliaLang#11310 (JuliaLang#22834)" This partially reverts commit c6e51d0.
…uliaLang#11310 (JuliaLang#22834)" This partially reverts commit c6e51d0.
…uliaLang#11310 (JuliaLang#22834)" This partially reverts commit c6e51d0.
…uliaLang#11310 (JuliaLang#22834)" This partially reverts commit c6e51d0.
…uliaLang#11310 (JuliaLang#22834)" This partially reverts commit c6e51d0.
…uliaLang#11310 (JuliaLang#22834)" This partially reverts commit c6e51d0.
…uliaLang#11310 (JuliaLang#22834)" This partially reverts commit c6e51d0.
…uliaLang#11310 (JuliaLang#22834)" This partially reverts commit c6e51d0.
…uliaLang#11310 (JuliaLang#22834)" This partially reverts commit c6e51d0.
tardy sytax
|
Hello, Does anyone know how to resolve the following issue: I did what it suggested and instead, I now get : Thanks! |
Please use the Discourse forum for questions, and keep GitHub for bug reports. Thanks! |
i.e. the method definition form fails but the
call
form works. It might be nice if the former was lowered to the latter – having them mean different things is counter-intuitive to me.Edit: Looks like the issue is the ambiguity of
Foo{T}() = ...
withcall{T}(::Type{Foo{T}}) = ...
andcall{T, S}(::Type{Foo{S}}) = ...
. If there's no way around this perhaps it should raise an ambiguity error with thecall
methods as suggestions.The text was updated successfully, but these errors were encountered: