https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17097754782012-08-15T02:06:59ZRuby Issue Tracking SystemRuby master - Feature #6868: Make `do` in block syntax optional when the block is the last argument of a method and is not an optional argumenthttps://redmine.ruby-lang.org/issues/6868?journal_id=288692012-08-15T02:06:59Zalexeymuranov (Alexey Muranov)
<ul></ul><p>I have just realized that parameters are parsed before looking up the method definition, so maybe this is not possible...</p>
<p>I was just thinking if it is possible to unify somehow all Ruby objects and classes. (No matter how hard <code>Class</code> pretends to be a simple object, it is clear that it is not only not a simple object, but not even a simple class, it cannot be subclassed for example.)</p> Ruby master - Feature #6868: Make `do` in block syntax optional when the block is the last argument of a method and is not an optional argumenthttps://redmine.ruby-lang.org/issues/6868?journal_id=288712012-08-15T05:24:08Zdrbrain (Eric Hodel)drbrain@segment7.net
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li><li><strong>Priority</strong> changed from <i>3</i> to <i>Normal</i></li></ul><p>=begin<br>
What would this code do:</p>
<p>def method n<br>
n = n.times<br>
p n<br>
n<br>
end</p>
<p>Currently it prints (({#<Enumerator: 3:times>})) and returns an Enumerator. Would it instead raise SyntaxError for an unexpected end?</p>
<p>=end</p> Ruby master - Feature #6868: Make `do` in block syntax optional when the block is the last argument of a method and is not an optional argumenthttps://redmine.ruby-lang.org/issues/6868?journal_id=288722012-08-15T06:02:25Zalexeymuranov (Alexey Muranov)
<ul></ul><p>You mean, what should <code>method(3)</code> output? This was not considered in my proposal, i think it should be the same. In fact, i understand that there are issues with making <code>def</code> a method, i was only proposing to make blocks look more like class and method definitions in certain circumstances. I do not remember if there are Ruby core methods that require a block, all that come to my mind have an optional block parameter. So here is a made-up example of the use of the proposed syntax:</p>
<p>def do_three_times(&block)<br>
3.times(&block)<br>
end</p>
<p>do_three_times<br>
puts "Hi"<br>
end</p>
<p>The interpreter would need to look up the method definition for <code>do_three_times</code> before parsing the parameters (i think this is different from the current behavior), see that there is a required block parameter at the end, and so to expect either <code>do</code> or just new line to start the block after <code>do_three_times</code>.</p> Ruby master - Feature #6868: Make `do` in block syntax optional when the block is the last argument of a method and is not an optional argumenthttps://redmine.ruby-lang.org/issues/6868?journal_id=288742012-08-15T08:43:48Zpedz (Perry Smith)pedz@easesoftware.com
<ul></ul><p>The idea seems extremely bad to me.</p> Ruby master - Feature #6868: Make `do` in block syntax optional when the block is the last argument of a method and is not an optional argumenthttps://redmine.ruby-lang.org/issues/6868?journal_id=288752012-08-15T10:11:02Zdrbrain (Eric Hodel)drbrain@segment7.net
<ul></ul><p>=begin<br>
It seems impossible to implement without fundamentally changing the feel of writing ruby.</p>
<p>Due to enumerators, currently your example results in a SyntaxError:</p>
<p>$ pbpaste | ruby20<br>
-:7: syntax error, unexpected keyword_end, expecting $end</p>
<p>How do we disambiguate between existing uses of (({&block})) and your proposal?</p>
<p>Currently a method with (({&block})) accepts an optional block argument that is captured to the local variable (({block})). Since a named block argument is always optional it can be used by methods that return Enumerators (such as creating an enumerator from an Enumerable internal to the object). Changing this to mean "a block argument is required" breaks such usage.</p>
<p>After solving the problem of specifying a method's signature to enable your special parsing, how do we look up this information at runtime if the method is dynamically defined or method_missing is involved?</p>
<p>How do we parse this example?</p>
<p>class C<br>
def method<br>
do_three_times<br>
puts "Hi"<br>
end<br>
end</p>
<pre><code>def initialize
# defines the do_three_times method in a manner that enables "do"-less syntax
require 'c/do_three_times'
end
</code></pre>
<p>end</p>
<p>C.new.do_three_times</p>
<p>The definition of (({do_three_times})) is ambiguous when the call is encountered, so the number of (({end}))s to consume is impossible to determine. Ruby can't look in (({c/do_three_times.rb})) to determine how to parse (({do_three_times})) since it is dynamic.</p>
<p>If the parser assumes (({do_three_times})) is a do-less block then (({C.new})) will require (({c/do_three_times.rb})). However, if that file defines (({do_three_times})) to be a method that returns an Enumerator (not a do-less block) then the file was required at the wrong spot (as the second (({end})) after (({puts})) closed C and #initialize was not part of class C so the require should not have happened.</p>
<p>I'm unsure how you are going to resolve ambiguity due to the dynamic nature of ruby absent a (({do})) or introduction of some other discriminator (such as python's significant whitespace).</p>
<p>=end</p> Ruby master - Feature #6868: Make `do` in block syntax optional when the block is the last argument of a method and is not an optional argumenthttps://redmine.ruby-lang.org/issues/6868?journal_id=288872012-08-15T16:33:42Zalexeymuranov (Alexey Muranov)
<ul></ul><p>Thanks for the explanation and for the last example with method call inside <code>def</code>, i did not think well enough. I see that keywords can be parsed because they are known to be keywords. I think this can be closed, sorry.</p> Ruby master - Feature #6868: Make `do` in block syntax optional when the block is the last argument of a method and is not an optional argumenthttps://redmine.ruby-lang.org/issues/6868?journal_id=288982012-08-16T02:35:27Zdrbrain (Eric Hodel)drbrain@segment7.net
<ul><li><strong>Status</strong> changed from <i>Feedback</i> to <i>Rejected</i></li></ul><p>Do not be sorry!</p>
<p>Adding syntax to a language is very difficult and touches all of the language. It Is great that you tried!</p>