https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112020-12-17T00:49:43ZRuby Issue Tracking SystemRuby master - Feature #17398: SyntaxError in endless methodhttps://bugs.ruby-lang.org/issues/17398?journal_id=892612020-12-17T00:49:43Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>The body of an endless method must be an expression (called "arg" in the syntax rules of parse.y). <code>puts("bar")</code> is an expression, but <code>puts "bar"</code> is a statement (called "command" in the syntax rules).</p>
<p>I think it could be a bit confusing, but I have no idea whether we can/should allow a statement as a body of an endless method.</p> Ruby master - Feature #17398: SyntaxError in endless methodhttps://bugs.ruby-lang.org/issues/17398?journal_id=892752020-12-17T09:39:18Zzverok (Victor Shepelev)zverok.offline@gmail.com
<ul></ul><p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/18">@mame (Yusuke Endoh)</a> Hmm, haven't thought about it from this perspective... Can you please explain a bit? As far as I can see, in, say, assignment context it behaves like an expression:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">result</span> <span class="o">=</span> <span class="nb">puts</span> <span class="s1">'foo'</span>
<span class="c1"># prints "foo", result = nil</span>
</code></pre>
<p>I am just trying to describe the behavior in full for the next installment of my <a href="https://rubyreferences.github.io/rubychanges/" class="external">changelog</a> and this aspect is quite confusing for me... Though, it is not endless-method specific, as far as I can see:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">y</span> <span class="o">=</span> <span class="n">sin</span> <span class="n">x</span> <span class="c1"># OK</span>
<span class="n">y</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">sin</span> <span class="n">x</span>
<span class="c1"># ^ unexpected local variable or method, expecting `do' or '{' or '('</span>
</code></pre>
<p>What's the "rule of thumb" to understand this better?</p> Ruby master - Feature #17398: SyntaxError in endless methodhttps://bugs.ruby-lang.org/issues/17398?journal_id=892762020-12-17T13:52:26ZEregon (Benoit Daloze)
<ul></ul><p>Conceptually, according to the typical definition in computer science, both <code>puts("bar")</code> and <code>puts "bar"</code> are expressions (i.e., they return a value, and if it was some other method than <code>puts</code> it would also not always be <code>nil</code>).<br>
It might be slightly less clear for e.g. <code>a = 42</code> (it's still an expression, it still returns a value), but I think <code>puts "bar"</code> is clear that it should be the same as <code>puts("bar")</code>, except for precedence.</p>
<p>So it's probably going to be very difficult to explain the actual condition, other than showing specific examples.</p> Ruby master - Feature #17398: SyntaxError in endless methodhttps://bugs.ruby-lang.org/issues/17398?journal_id=892782020-12-17T16:16:37Zaustin (Austin Ziegler)halostatue@gmail.com
<ul></ul><p>Eregon (Benoit Daloze) wrote in <a href="#note-3">#note-3</a>:</p>
<blockquote>
<p>So it's probably going to be very difficult to explain the actual condition, other than showing specific examples.</p>
</blockquote>
<p>Endless methods definitions don’t support poetry mode?</p> Ruby master - Feature #17398: SyntaxError in endless methodhttps://bugs.ruby-lang.org/issues/17398?journal_id=892832020-12-18T01:58:13Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>The following patch allows <code>def foo() = puts "bar"</code>. It brings no parser conflict.</p>
<p><a href="https://gist.github.com/mame/0773bf3938e046e2b608de5fb2a826c8" class="external">https://gist.github.com/mame/0773bf3938e046e2b608de5fb2a826c8</a></p>
<p>However, it is not perfect. <code>private def foo() = puts "foo"</code> does not parse.<br>
<code>private var = puts "bar"</code> is not allowed neither, so I have no idea how to allow this.</p> Ruby master - Feature #17398: SyntaxError in endless methodhttps://bugs.ruby-lang.org/issues/17398?journal_id=915662021-04-16T04:30:30Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Tracker</strong> changed from <i>Bug</i> to <i>Feature</i></li><li><strong>Backport</strong> deleted (<del><i>2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN</i></del>)</li></ul><p>Pros</p>
<ul>
<li>More intuitive / consistent / natural</li>
</ul>
<p>Cons</p>
<ul>
<li>Duplicated syntax rules</li>
<li>Even more complex syntax</li>
</ul>
<p>If I were young, I would add @mame's patch. I did similar decisions many times in the past. But Ruby has been mature and complex, I now feel reluctant. Let us consider this idea for a while.</p>
<p>Matz.</p> Ruby master - Feature #17398: SyntaxError in endless methodhttps://bugs.ruby-lang.org/issues/17398?journal_id=916092021-04-19T05:06:06Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>File</strong> <a href="/attachments/8833">allow-command-style-endless-method-def.patch</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/8833/allow-command-style-endless-method-def.patch">allow-command-style-endless-method-def.patch</a> added</li></ul><p>I'm attaching an updated patch with a test.</p> Ruby master - Feature #17398: SyntaxError in endless methodhttps://bugs.ruby-lang.org/issues/17398?journal_id=919102021-05-12T08:32:01Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>I have considered this issue for a while and concluded it should be merged to be consistent with the assignment statement.</p>
<p>Matz.</p> Ruby master - Feature #17398: SyntaxError in endless methodhttps://bugs.ruby-lang.org/issues/17398?journal_id=919162021-05-12T15:17:49Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset <a class="changeset" title="parse.y: Allow "command" syntax in endless method definition This change allows `def hello = put..." href="https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/31794d2e733e081e4e221f27aff6380393981681">git|31794d2e733e081e4e221f27aff6380393981681</a>.</p>
<hr>
<p>parse.y: Allow "command" syntax in endless method definition</p>
<p>This change allows <code>def hello = puts "Hello"</code> without parentheses.</p>
<p>Note that <code>private def hello = puts "Hello"</code> does not parse for<br>
technical reason.</p>
<p>[Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: SyntaxError in endless method (Closed)" href="https://bugs.ruby-lang.org/issues/17398">#17398</a>]</p>