Crystal: `next` accepts named arguments

Created on 16 Jul 2018  路  6Comments  路  Source: crystal-lang/crystal

Arguments to next are parsed like arguments to method calls, but they shouldn't support named parameters:

next 1, foo: 2

This should probably raise a syntax error. Could also capture the named arguments and provide them somehow to the yielding scope, but that's probably too complicated - and not really useful.

If only named arguments are provided next foo: 2, it raises Index out of bounds.

bug compiler topicparser

Most helpful comment

If no positional arguments are present, named arguments could perhaps be transformed to a named tuple. So next foo: bar would be equal to next {foo: bar}. But it's not a huge difference, so probably not worth it to have this special case.

All 6 comments

That's also happening at break.

return statement accepts named arguments (of course not work). Because they are parsed by same parse_control_expression method.

Additionally yield accepts named arguments also, but we cannot refer these values. It should be fixed.

If no positional arguments are present, named arguments could perhaps be transformed to a named tuple. So next foo: bar would be equal to next {foo: bar}. But it's not a huge difference, so probably not worth it to have this special case.

All of this works in Ruby, I would do nothing regarding this for now.

foo = while true
        break 1, foo: 1
      end
p foo

In Ruby it yields [1, {foo: 1}], but in Crystal it yields what?

Wow! Crystal ignore break and next value inside while loop... #1277

Was this page helpful?
0 / 5 - 0 ratings