From: shevegen@... Date: 2018-12-29T12:19:33+00:00 Subject: [ruby-core:90804] [Ruby trunk Feature#15483] Proc or Method combination with Symbol Issue #15483 has been updated by shevegen (Robert A. Heiler). I am biased so I do not want to digress from this thread too much while explaining my bias. However had, I still want to state a few things: - In regards to Symbol, this is a language design decision, how Symbols are to be used. I think we can have valid arguments for both main variants, e. g. to keep Symbols simple, or to allow more flexibility. Personally I'd rather prefer them simple, largely because I don't feel most proposals for change make them better and most definitely not prettier; but I have no real problem either way here. Still, in regards to proposals allowing for more flexibility of Symbols, this leads me to: - **Syntax consideration**. To me personally the proposed syntax is not very elegant. In particular: .map(&(&:to_i >> &:chr)) Is really not pretty. We use '& three' times there; and the new >>. It does not really feel consistent with other parts of ruby in my opinion, syntax-wise alone. I have less of a problem with a single & but I also dislike that I have to look carefully, e. g to distinguish between a** .map(&:)** versus a **.map(&)** variant. Do we really want to have to look for & now carefully and a : or no :, on top of it? The second variant also packs a lot more information into the method-call, which makes it a bit hard to see what is going on to me, e. g. **.map(&(&:to_i >> :chr.to_proc)))**. And the >> which I am also not a big fan of, but as said in the beginning, I am biased already, so my comments will be biased as well. - Another issue I have, and this is more general, that I do not really see the massive benefit. This is not solely confined to the proposal here, and is obviously subject to personal opinion/evaluation and how you use ruby ("more than one way to use ruby", too), but more generally about some other related proposals too, where I am not really sure if the change is needed or provides a lot of really useful things that we need. I understand it if the goal is more flexibility in what we can do; for example, I think I also stated before that I am in agreement with proposals to allow arguments to methods given rather than solely be able to use e. g. .map(&:method SOME WAY FOR ARGUMENTS HERE). The major problem I have with most of these proposals I have seen so far is syntax-wise. We do not have that many characters while staying in ASCII land, but the core of ruby is very elegant and quite simple, syntax-wise (for me). Several of the proposals in the last ~3 years or so, are, to me, syntax-wise, not really elegant. Syntax is not everything but if I have to stare at code a lot then I'd rather look at good syntax than bad one. Anyway, I'll close my comment here. ---------------------------------------- Feature #15483: Proc or Method combination with Symbol https://bugs.ruby-lang.org/issues/15483#change-75978 * Author: aycabta (aycabta .) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- In [Feature #6284], Matz said > We need more discussion if we would add combination methods to the Symbol class. Right, let's get started to discuss. For your information, recent a few months I'm discussing this with @osyo . ## This is a discussion of "design" I understand that all features of this issue have both merits and demerits, but I guess that language design is most important. All features of this issue related to each other. ## Abstract At present, you can use `Proc#>>` or `Proc#<<` with `Symbol#to_proc`. ```ruby %w{72 101 108 108 111}.map(&(:to_i.to_proc >> :chr.to_proc)) # => ["H", "e", "l", "l", "o"] ``` This is convenient but methods that take block can take a proc with `&` syntax sugar instead of `#to_proc` by right, like `[1, 2, 3].map(&:to_s)`. So `Symbol#to_proc` looks like too long for `Proc#>>` or `Proc#<<`. Therefore, you need new syntax sugar. ## Receiver ### `Symbol#>>` and `Symbol#<<` `Symbol#>>` and `Symbol#<<` will be considered, but this means that `Symbol` is treated as `Proc` partially. The `[1, 2, 3].map(&:to_s)` treats `Symbol` as `Proc` partially too, but it's with pre-positioned `&`. ```ruby %w{72 101 108 108 111}.map(&(:to_i >> :chr.to_proc)) # => ["H", "e", "l", "l", "o"] ``` I can't come up with other ideas for the `Symbol` receiver. ### New `&:symbol_name` syntax sugar for `:symbol_name.to_proc` ```ruby %w{72 101 108 108 111}.map(&(&:to_i >> :chr.to_proc))) # => ["H", "e", "l", "l", "o"] ``` ## Argument ### Calls `#to_proc` by `Proc#>>` or `Proc#<<` internally as a duck typing ```ruby %w{72 101 108 108 111}.map(&(:to_i.to_proc >> :chr)) # => ["H", "e", "l", "l", "o"] ``` In this case, `Proc#>>`(`:to_i.to_proc >>`) calls `Symbol#to_proc`(for `:chr`) inside. This is useful to use with `Hash#to_proc`: ```ruby h = { Alice: 30, Bob: 60, Cris: 90 } %w{Alice Bob Cris}.map(&(:to_sym.to_proc >> h)) # => [30, 60, 90] ``` ### `Proc#>>` and `Proc#<<` take block as an argument ```ruby %w{72 101 108 108 111}.map(&(:to_i.to_proc >> &:chr)) ``` ## Combination of receiver and argument `Symbol#>>` and calling `#to_proc` internally: ```ruby %w{72 101 108 108 111}.map(&(:to_i >> :chr)) # => ["H", "e", "l", "l", "o"] ``` `&:symbol_name` syntax sugar for `:symbol_name.to_proc` and `Symbol#>>` and taking block: ```ruby %w{72 101 108 108 111}.map(&(&:to_i >> &:chr)) # => ["H", "e", "l", "l", "o"] ``` -- https://bugs.ruby-lang.org/ Unsubscribe: