-
-
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
deprecate special "inner constructor" syntax #20308
Conversation
eea6f4b
to
6ed81a0
Compare
|
There are a few syntax proposals out there. I think some options are
|
Is there a nice way to write code that does not hit a deprecation in 0.6, but still works with 0.5 and possibly even 0.4? type Foo{T}
x::T
(::Type{Foo{T}}){T}(x::T) = new{T}(x)
end It does not work on 0.4, though. It parses, however, so is it worth having type Foo{T}
x::T
@compat (::Type{Foo{T}}){T}(x::T) = new{T}(x)
end (or another macro in Compat) do the right thing? Obviously, supporting the 0.6 syntax would be much better, but as the |
|
Ah, of course, Compat already does this. I less convoluted way of doing this would be nice, but looks like this is indeed usable. Sorry for the noise, then. |
What's the drawback of |
That does seem pretty good for short-form definitions. |
Not sure if anyone have pointed it out but |
Another related issue:
currently parses as
which is pretty weird. I think we'd have to allow
but that introduces a space sensitivity. |
It doesn't seem unreasonable to discourage (or maybe even disallow) method type assertions within short form definitions. If you want to use a method type assertion, you can use the long form… where that special parsing does seem to make sense. |
The function (x::T,) where T
...
end There has always been an analogy between tuples and function arguments, and the function ((x::T,) where T)::T
...
end (you could wrap the function (f(x::T) where T)::T
...
end So IMO Jeff's first example should be (f(x::T) where T)::T = ... Would that parse? EDIT: Sorry, I got muddled in the last code snippet, fixed. |
I've been thinking - another way to perhaps make this more obvious, as well as the case I'm sure this was brought up once before, but I can't find it right now. Consider the four ways of defining named/anoymous short/long functions in Julia: f(x,y) = x + y
(x,y) -> x + y
function f(x,y); x + y; end
function (x,y); x + y; end It is the first one which is the odd-one out (and we can't put
which seems more like a function than some |
The snippet |
True. I guess we have the same problem with the current syntax looking like |
That doesn't mean the same thing.
I find this too weird --- the last |
Oh right! Oops, I assumed the Carry on... |
To me, it seemed similar to But yes, that syntax does look weird. |
This is somewhat continuing the derailing of the PR, but could we make the return type more similar to other languages and put it before the function name? i.e. # long-form
function Void f(x, y)
end
# long-form w/ parameters
function T f(x::T, y::T) where T
end
# short-form
Void f(x,y) = ...
# short-form
T f(::T, y::T) where (T) = ... This would be, of course, orthogonal to whether we keep |
e5242bd
to
b811390
Compare
I've updated this to use @vtjnash 's suggestion (I think @Keno also suggested this once) of writing method parameters in curly braces, e.g.
|
So now the short function form is a bit less confusing? f(::T) where {T} = T Looks a bit better than having PS full symmetry is this: P = Pair{A, B} where {A<:Real, B<:Real}
f(x::Pair{A, B}) where {A<:Real, B<:Real}
struct P{A, B} where {A<:Real, B<:Real}
...
end but I do love what you have already, Jeff. (Is there some constraints on struct type params that can be expressed in this form but not the existing? |
I don't believe so. What it would let you express is type parameters that are fixed at a certain value, like
i.e. the third parameter is always |
OK, thanks. |
+1 with andy for the full symmetry syntax. Maybe an idea for the where clause readability issue with T=T : WHERE CLAUSE @ ONELINER FUNCTION FORM
or
WHERE CLAUSE @ MANYLINER FUNCTION FORM
Anyway, thanks you for all the work already done. |
b811390
to
6791752
Compare
Nullable() = new(false) | ||
Nullable(value::T, hasvalue::Bool=true) = new(hasvalue, value) | ||
Nullable{T}() where T = new(false) | ||
Nullable{T}(value::T, hasvalue::Bool=true) where T = new(hasvalue, value) |
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.
some form of brackets here, please
The readability request, made multiple times by multiple people, was not to have |
We will all be happier if we just get used to it. |
Ok, I will add braces to most of these. |
There's no line number in the deprecation warning for this, see for example the one from |
No description provided.