Crystal: Cannot pass hash directly to a method without parentheses

Created on 25 Jun 2019  路  4Comments  路  Source: crystal-lang/crystal

def do_work(hash : Hash(String, String)); end

do_work {"foo" => "bar"}

Syntax error in eval:3: unexpected token: =>

Assigning the hash to a variable first or adding parentheses to the method invocation works.

Most helpful comment

Duplicate of #6224, #2746, #4826, #1051

All 4 comments

I guess that's because parentheses are also used for blocks.

Yes, this a limitation in the grammar.

To expand on why this is unsolvable, otherwise the compiler will need to special-case this with 3-level deep lookahead. Which is usually seen as a very bad thing (sorry for not expanding on that one).

do_work { could be both
do_work {"foo" could be both
do_work {"foo" = could be both
do_work {"foo" => ok right, now I can go back and actually parse this as Hash

Duplicate of #6224, #2746, #4826, #1051

Was this page helpful?
0 / 5 - 0 ratings