From: pvande@... Date: 2018-03-26T21:43:32+00:00 Subject: [ruby-core:86315] [CommonRuby Feature#13581] Syntax sugar for method reference Issue #13581 has been updated by pvande (Pieter van de Bruggen). As a blue sky alternative, why not consider something like this: ~~~ ruby [1, 2, 3].map(&> {Math.sqrt}) # or perhaps more simply [1, 2, 3].map(&> Math.sqrt) ~~~ Pros: * It's clean * It's readable * It reads like passing a block * Specifically, passing a lambda as a block * It *also* reads something like the Elixir pipe operator Cons: * It's only *looks* like Ruby code * Is `x.map(&> { a.b.c.d ; Math.sqrt })` valid? (I hope not.) * Is `x.map(&> do; Math.sqrt; end)` valid? (I hope not.) * Is `x.map(&> { begin; rescue; end })` valid? (I hope not.) * Is `x.map(&> { Math.sqrt if x })` valid? (I hope not.) * Is `x.map(&> { Math.sqrt rescue nil })` valid? (I hope not.) * It's not actually a shorthand for `Object#method`. * The two clearest implementation paths are as a "macro", and as an "atypical evaluation context" * The macro approach simply transforms the code into a "proper" block, and passes the argument implicitly (see below for an example) * The other approach requires the interpreter to all non-terminal method calls, then produce a block invoking the terminal call with the yielded argument (see below for an example) Despite the "oddness" of this particular syntax, I think the clarity of expression is very much inline with the Ruby ideals, and is therefore worth discussing. **Macro Example** ~~~ruby fn(&> { a.b.c.d }) # => fn() { |__x| a.b.c.d(__x) }) ~~~ **Atypical Evaluation Example** ~~~ruby fn(&> { a.b.c.d }) # => __target = a.b.c; fn() { |__x| __target.d(__x) }) ~~~ ---------------------------------------- Feature #13581: Syntax sugar for method reference https://bugs.ruby-lang.org/issues/13581#change-71235 * Author: americodls (Americo Duarte) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- Some another programming languages (even Java, in version 8) has a cool way to refer a method as a reference. I wrote some examples here: https://gist.github.com/americodls/20981b2864d166eee8d231904303f24b I miss this thing in ruby. I would thinking if is possible some like this: ~~~ roots = [1, 4, 9].map &Math.method(:sqrt) ~~~ Could be like this: ~~~ roots = [1, 4, 9].map Math->method ~~~ What do you guys thinking about it? -- https://bugs.ruby-lang.org/ Unsubscribe: