-
-
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
Error in: f(x::T)::Int where T = x #21847
Comments
Yes, this is tricky, since |
It's easy to just parenthesize the function and return type. |
Quoting from #23163:
Or at least throwing an error when the short form is used with a type annotation and a where clause – you can't recover once you've reached the |
What would the error suggest, those features can only be used together in long form functions? Should probably only disallow the ambiguous / confusing cases, if things are parenthesized such that the intent is clear, I don't see a reason to error. |
Yes, it would only the be necessary to raise an error when there are no disambiguating parens. @bramtayl: what do you suggest as an alternative? I see only four plausible options:
Option 1 seems clearly bad and 4 is not going to happen, which leaves only the middle two. |
Hmm. I wasn't reading closely, I was just immediately off-put because I almost exclusively use short form functions (with begin blocks where necessary), and in the long term it would be nice to maintain the same functionality for short form and long form. |
Maybe the parser could detect this specific situation and provide a descriptive error message that points to this issue? Something like: Currently (Julia 1.7.1) I'm getting this confusing error: julia> is_almost_zero(x::T)::Bool where T<:Real = x ≈ zero(T)
ERROR: UndefVarError: T not defined
Stacktrace:
[1] top-level scope
@ REPL[27]:1 It doesn't even tell me which particular |
I came here from this discourse thread (thanks @fredrikekre). After reading through, seems like the problem can technically be solved with
While I agree that it is more natural to have the denotation of a type go first, I would however disagree that this is the role of Let me reframe using this analogy to bring out the intuition further: In mathematics when we talk about "x^2 + 2x > -1 for x in real", we mean "(x^2 + 2x > -1) for (x in real)". The denotation of the set of x is "x is real". The "for" is used to scope the whole expression and not to x. This analogy is consistent with our existing definition, e.g.:
The type denotation is actually just So I think it is still natural to have:
to get the precedence of:
|
Personal thought: Visually, at least, the expression that must be separated by Spaces likes |
This would be breaking to change at this point |
In other words,
::ReturnType
is incompatible withwhere
clauses in the short function form declarations.I thought an exemple was more clear than my words in the title.
The text was updated successfully, but these errors were encountered: