Julia: Error in: f(x::T)::Int where T = x

Created on 13 May 2017  路  6Comments  路  Source: JuliaLang/julia

In other words, ::ReturnType is incompatible with where clauses in the short function form declarations.
I thought an exemple was more clear than my words in the title.

parser

All 6 comments

Yes, this is tricky, since a where b denotes a type, and x::t means x has type t, so x::a where b is most naturally parsed as x::(a where b).

It's easy to just parenthesize the function and return type.

Quoting from #23163:

What about disallowing type annotations on short-form functions entirely? They're not super useful anyway. Throwing an error would be better here than giving a surprising interpretation.

Or at least throwing an error when the short form is used with a type annotation and a where clause 鈥撀爕ou can't recover once you've reached the = but at least it can throw a helpful error.

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:

  1. Do nothing and continue to parse this as we currently do.
  2. Raise a clear error when the parser encounters this specific situation.
  3. Disallow return type annotations on short-form methods altogether.
  4. Change the parser to support infinite backtracking.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arshpreetsingh picture arshpreetsingh  路  3Comments

omus picture omus  路  3Comments

dpsanders picture dpsanders  路  3Comments

StefanKarpinski picture StefanKarpinski  路  3Comments

sbromberger picture sbromberger  路  3Comments