Skip to content
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

Use new where syntax in misc files #21428

Merged
merged 32 commits into from
Apr 21, 2017
Merged

Conversation

musm
Copy link
Contributor

@musm musm commented Apr 18, 2017

No description provided.

@tknopp
Copy link
Contributor

tknopp commented Apr 18, 2017

Out of curious: Is this now the recommended form for parameterized functions?

@yuyichao
Copy link
Contributor

The old version might be deprecated/replaced in 1.0 so that it can be used to define outer constructors.

@tknopp
Copy link
Contributor

tknopp commented Apr 18, 2017

This is a pretty fundamental change. The old syntax was pretty readable IMHO. I thought that where is only required for complex relations that could not be expressed before.

@musm
Copy link
Contributor Author

musm commented Apr 18, 2017

This is a pretty fundamental change. The old syntax was pretty readable IMHO. I thought that where is only required for complex relations that could not be expressed before.

The new syntax helps clarify parameterized function and parameterized constructors and IMO is a lot more clear and less confusing now.

@tknopp
Copy link
Contributor

tknopp commented Apr 18, 2017

Could you give a link where this has been decided? In my point of view the former syntax was clear and readable, whereas the trailing notation requires looking back and forth (i.e. is less readable)

@musm
Copy link
Contributor Author

musm commented Apr 18, 2017

Could you give a link where this has been decided? In my point of view the former syntax was clear and readable, whereas the trailing notation requires looking back and forth (i.e. is less readable)

Very heavily discussed here #18457

The new rule is a lot more simple: if there is a where then {T} is now a literal; e.g. things such as the following are a lot less confusing in 0.6 with the where syntax

type X{T}; end
X{T}(::T) = X{T}()

type Y{T}; end
Y{T}(::T) where {T} = Y{T}()

@JeffBezanson
Copy link
Member

What does the following definition do?

Complex{Float64}(x) = ...

It shouldn't be controversial that this looks like it defines a constructor for the type Complex{Float64}, with one argument of any type. But it doesn't do that --- it defines a constructor for the type Complex, with a static parameter called Float64 that doesn't occur anywhere, so the method is not actually callable. This syntax was pretty clearly a mistake.

See discussion starting here: #10146 (comment)
Also issue #11310 which has most of the discussion
A bit of #8135

@tknopp
Copy link
Contributor

tknopp commented Apr 18, 2017

I partly understand this issue within constructors. But this is a quite local issue in contrast to parametrized functions that occur a lot more often than constructors. I had a look at the above patch and it seems that 100% of the changes did not involve a constructor.

@JeffBezanson
Copy link
Member

That is largely because this syntax change has already been done for constructors.

@JeffBezanson
Copy link
Member

You're right that non-constructors occur much more often than constructors, but the syntax should still be consistent. We don't want A{B} to define a constructor for A{B} if A is a type, and otherwise introduce a static parameter. That would be both confusing and unnecessarily difficult to implement.

@tknopp
Copy link
Contributor

tknopp commented Apr 18, 2017

I see the consistency issue here. I am not sure if this outweighs the regression in readability. One could also see the old syntax as a terse form for the new syntax and constructors are simply too complex to allow for a terse form.

@JeffBezanson
Copy link
Member

Not everybody agrees that there is any regression in readability. For instance I found the examples in #11310 (comment) compelling in favor of the new syntax.

Arguably, the list of static parameters and their bounds is not the first thing you want to see. Both the old and new syntax group together (1) the arguments, (2) the static parameter bounds, so with either syntax you might have to look back and forth. There is also a new syntax that combines them:

f(x::Vector{<:Real}, y::Vector{<:Number}) = ...

In that case there is truly no looking back and forth.

Some also find that the where is a nice visual separation in what used to be a dense nest of punctuation, e.g. (::Type{T}){T}(arg) vs. (::Type{T})(arg) where T.

@tknopp
Copy link
Contributor

tknopp commented Apr 18, 2017

ok, maybe I simply need to get used to that. Thanks for the explanation.

@musm musm force-pushed the where branch 4 times, most recently from 8f6eca2 to a961492 Compare April 19, 2017 00:51
@BobPortmann
Copy link
Contributor

So could the first line in this pull request

function indices{T,N}(A::AbstractArray{T,N}, d)

become

function indices(A::AbstractArray{<:Any,<:Any}, d)

instead of

function indices(A::AbstractArray{T,N}, d) where {T,N}

@musm
Copy link
Contributor Author

musm commented Apr 19, 2017

No because we need N in the function https://github.com/JuliaLang/julia/pull/21428/files#diff-2264bb51acec4e7e2219a3cb1c733651R40

I suppose this would work
function indices(A::AbstractArray{<:Any,N}, d) where N
but I think it's more readable with both explicit in this particular instance ?

@BobPortmann
Copy link
Contributor

Yes, of course

But I do agree with @tknopp that the new syntax is overly verbose for ordinary functions.

@musm
Copy link
Contributor Author

musm commented Apr 19, 2017

I think once you get used to the syntax you start appreciating it, give it a shot first before dismissing it. It took me awhile to get used to the current syntax and how it works, but now it feels natural, so I think familiarity has strong impact on preference.
e.g. I really appreciate the new syntax for things such as
*(x::T,y::T) where {T<:Integer} #....
vs.
*{T<:Integer}(x::T,y::T) #....

@musm musm force-pushed the where branch 8 times, most recently from aaa528e to 18ec763 Compare April 19, 2017 13:34
base/pair.jl Outdated
@@ -35,5 +35,5 @@ function convert(::Type{Pair{A,B}}, x::Pair) where A where B
Pair{A, B}(convert(A, x[1]), convert(B, x[2]))
end

promote_rule{A1, B1, A2, B2}(::Type{Pair{A1, B1}}, ::Type{Pair{A2, B2}}) =
promote_rule(::Type{Pair{A1, B1}}, ::Type{Pair{A2, B2}}) where {A1, B1, A2, B2} =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra spaces?

@@ -298,7 +298,7 @@ Base.eltype{T<:Period}(::Type{T}) = T
Base.promote_rule(::Type{Date}, x::Type{DateTime}) = DateTime
Base.isless{T<:TimeType}(x::T, y::T) = isless(value(x), value(y))
Base.isless(x::TimeType, y::TimeType) = isless(Base.promote_noncircular(x, y)...)
=={T<:TimeType}(x::T, y::T) = ==(value(x), value(y))
==(x::T, y::T) where {T<:TimeType} = ==(value(x), value(y))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Unrelated, but the } = ==( tripped up my wetware parser. Perhaps infix == would be better?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, but I think this is better for a separate PR

@@ -72,7 +72,7 @@ print(io::IO, t::Text) = print(io, t.content)
print(io::IO, t::Text{<:Function}) = t.content(io)
show(io::IO, t::Text) = print(io, t)

=={T<:Union{HTML, Text}}(t1::T, t2::T) = t1.content == t2.content
==(t1::T, t2::T) where {T<:Union{HTML, Text}} = t1.content == t2.content
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space?

base/test.jl Outdated
a::StridedVecOrMat{S}, b::StridedVecOrMat{T},
err = length(indices(a,1))^3*(eps(S)+eps(T))
)
) where {S<:Real,T<:Real}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent seems odd?

@@ -25,17 +25,17 @@ const BitUnsigned64T = Union{Type{UInt8}, Type{UInt16}, Type{UInt32}, Type{UInt6

## integer comparisons ##

<{T<:BitSigned}(x::T, y::T) = slt_int(x, y)
(<)(x::T, y::T) where {T<:BitSigned} = slt_int(x, y)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space or alignment here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no this is supposed to be aligned with the code in the lines below

promote_to_supertype{T<:Number,S<:Number}(::Type{T}, ::Type{S}, ::Type{T}) = (@_pure_meta; T)
promote_to_supertype{T<:Number,S<:Number}(::Type{T}, ::Type{S}, ::Type{S}) = (@_pure_meta; S)
promote_to_supertype{T<:Number,S<:Number}(::Type{T}, ::Type{S}, ::Type) =
promote_to_supertype(::Type{T}, ::Type{T}, ::Type{T}) where {T<:Number } = (@_pure_meta; T)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the injection of spaces for alignment would be better between the } and =?

@@ -137,15 +137,15 @@ end

## Multiplication by Q
### QB
A_mul_B!{T<:BlasFloat}(A::LQPackedQ{T}, B::StridedVecOrMat{T}) = LAPACK.ormlq!('L','N',A.factors,A.τ,B)
A_mul_B!(A::LQPackedQ{T}, B::StridedVecOrMat{T}) where {T<:BlasFloat} = LAPACK.ormlq!('L','N',A.factors,A.τ,B)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra spaces?


function dot{T<:BlasReal, TI<:Integer}(x::Vector{T}, rx::Union{UnitRange{TI},Range{TI}}, y::Vector{T}, ry::Union{UnitRange{TI},Range{TI}})
function dot(x::Vector{T}, rx::Union{UnitRange{TI},Range{TI}}, y::Vector{T}, ry::Union{UnitRange{TI},Range{TI}}) where {T<:BlasReal, TI<:Integer}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space?

@@ -56,7 +56,7 @@ function dot{T<:BlasReal, TI<:Integer}(x::Vector{T}, rx::Union{UnitRange{TI},Ran
BLAS.dot(length(rx), pointer(x)+(first(rx)-1)*sizeof(T), step(rx), pointer(y)+(first(ry)-1)*sizeof(T), step(ry))
end

function dot{T<:BlasComplex, TI<:Integer}(x::Vector{T}, rx::Union{UnitRange{TI},Range{TI}}, y::Vector{T}, ry::Union{UnitRange{TI},Range{TI}})
function dot(x::Vector{T}, rx::Union{UnitRange{TI},Range{TI}}, y::Vector{T}, ry::Union{UnitRange{TI},Range{TI}}) where {T<:BlasComplex, TI<:Integer}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space?

@@ -133,18 +133,18 @@ function typename(a::Union)
end
typename(union::UnionAll) = typename(union.body)

convert{T<:Tuple{Any,Vararg{Any}}}(::Type{T}, x::Tuple{Any, Vararg{Any}}) =
convert(::Type{T}, x::Tuple{Any, Vararg{Any}}) where {T<:Tuple{Any,Vararg{Any}}} =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent / extra space?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This space was already there but I might as well also fix this thanks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheers :)

Copy link
Member

@Sacha0 Sacha0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much thanks for slogging through this @musm! Generally looks great.

(Abstracting from this PR, developing a consistent style guideline for when to use e.g. where {S,T} rather than where S where T in long-form method definitions involving more than one type parameter seems worthwhile. After reading through this diff, I lean towards using the compact form consistently.)

@musm
Copy link
Contributor Author

musm commented Apr 21, 2017

(Abstracting from this PR, developing a consistent style guideline for when to use e.g. where {S,T} rather than where S where T in long-form method definitions involving more than one type parameter seems worthwhile. After reading through this diff, I lean towards using the compact form consistently.)

Agreed, although I don't have a strong pref on the convention, but that we should have some guideline

Also thank you for going through this. I pushed a commit to address reviews. Can you please take another look to give it a final g2g.

imported::Bool = false,
ambiguous_bottom::Bool = false,
allow_bottom::Union{Bool,Void} = nothing)
if allow_bottom !== nothing
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated but shoould be !== not !=

if allow_bottom != nothing
imported::Bool = false,
ambiguous_bottom::Bool = false,
allow_bottom::Union{Bool,Void} = nothing)
Copy link
Contributor Author

@musm musm Apr 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this how we break spacing ? I don't see anywhere else where we put the closing ) on a seperate line

@Sacha0
Copy link
Member

Sacha0 commented Apr 21, 2017

I pushed a commit to address reviews. Can you please take another look to give it a final g2g.

Checked out the touchup commit (#32) --- lgtm! :)

Should this be squashed?

Some squashing seems in order. Though how much squashing would be best I don't know. Perhaps someone wiser in this regard will weigh in?

@musm
Copy link
Contributor Author

musm commented Apr 21, 2017

I think it might just make the most sense to squash all the commits, I can't see any benefit otherwise

@musm
Copy link
Contributor Author

musm commented Apr 21, 2017

can we merge this now?

@StefanKarpinski StefanKarpinski merged commit 5482f91 into JuliaLang:master Apr 21, 2017
@musm musm deleted the where branch April 21, 2017 16:57
rofinn pushed a commit to invenia/julia that referenced this pull request Apr 21, 2017
@tkelman
Copy link
Contributor

tkelman commented Apr 23, 2017

The changes here in deprecated.jl made Lint.jl and LanguageServer.jl start failing their tests - https://github.com/JuliaCI/pkg.julialang.org/blob/3e484195ef2fcccae5f1447447c3590d142e570e/logs/Lint_0.6.log

That's a bug in those packages, but in the interest of not breaking things that were previously passing right before an RC, we should probably revert the part of this that changed deprecated.jl for now.

@musm
Copy link
Contributor Author

musm commented Apr 23, 2017

cc @TotalVerb for Lint.jl
is there a log for LanguageServer ?

@tkelman
Copy link
Contributor

tkelman commented Apr 23, 2017

@TotalVerb
Copy link
Contributor

I'll have a fix in Lint. It's probably that lint is trying to parse Base's deprecated.jl file, and doesn't understand where.

@musm
Copy link
Contributor Author

musm commented Apr 23, 2017

tahnks @TotalVerb !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants