Issue #18040 has been updated by jeremyevans0 (Jeremy Evans).


bughit (bug hit) wrote in #note-5:
> jeremyevans0 (Jeremy Evans) wrote in #note-4:
> > You may want to read the definition of `stmt` in parse.y, particularly this part: https://github.com/ruby/ruby/blob/eed5e8f796ab18e2e0a436dab83e35504ae3ded0/parse.y#L1481
> 
> You can call it a statement all you want but if it produces a value, its an expression. And it does produce a value.

The reason that `foo(1 if false)` is a syntax error is due to a parsing failure.  The parser considers it a statement (`stmt`) and not an expression.  What the parser considers statements can still return values in Ruby.

In any case, the reason for the issue remains the same.  The parser doesn't allow a statement (`stmt`) to be used directly as a method argument.  You have to wrap it in parentheses to use it as a method argument.  Trying to support a `stmt` used directly as a method argument (`arg`), results in 132 shift/reduce conflicts and 2056 reduce/reduce conflicts in the parser. I doubt there is any way to support it as a general method argument.  As I mentioned it may be possible to get it to work in only the single argument case by special casing it.  However, I couldn't get that to work without shift/reduce conflicts.  Maybe @nobu would have more luck.  Even then, we would have to decide if we want to support that, because it would result in inconsistent syntax, since `foo(1 if false)` would be allowed, but `foo(1 if false, 2)` (the multiple argument case) would not. 

----------------------------------------
Feature #18040: Why should `foo(1 if true)` be an error?
https://bugs.ruby-lang.org/issues/18040#change-92958

* Author: bughit (bug hit)
* Status: Open
* Priority: Normal
----------------------------------------
There's no ambiguity here that should require another set of parens `foo((1 if true))`



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>