[ruby-core:113918] [Ruby master Bug#15428] Refactor Proc#>> and #<<
From:
"Dan0042 (Daniel DeLorme) via ruby-core" <ruby-core@...>
Date:
2023-06-16 00:27:08 UTC
List:
ruby-core #113918
Issue #15428 has been updated by Dan0042 (Daniel DeLorme).
Maybe it was already obvious to everyone, but I feel that something important has not been mentioned in this discussion: `(a >> b).call(x)` is equivalent to `b.call(a.call(x))` but **neither a nor b needs to be a Proc**. Either could be a Method, for example. So coercing the argument to Proc is neither needed nor desirable. In the same way that `"" + 2` is a TypeError because `2` is not implicitly converted, `myproc >> :symbol` is a TypeError because `:symbol` is not implicitly converted. That's fully consistent with Ruby as a strongly typed language.
I think the proper solution here is `JSON.:parse >> .:symbolize_keys`
... we need to resurrect #12125 and #16264 but with better syntax and a more complete picture around FP.
----------------------------------------
Bug #15428: Refactor Proc#>> and #<<
https://bugs.ruby-lang.org/issues/15428#change-103571
* Author: zverok (Victor Shepelev)
* Status: Open
* Priority: Normal
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
#6284 introduced `Proc#>>` and `Proc#<<`, but the requirements to the argument is totally inconsistent with ANY other place in Ruby.
Currently, it is the **only** place in Ruby where coercing argument to `Proc` is done with `#call` method. Everywhere else it is done with `#to_proc`, and `#call` method never had any special significance except for `.()` sugar. I believe there are two possible actions:
1. change `#>>` and `#<<` to use `#to_proc` (which will give Symbols composability for free), **or, alternatively**
2. state that `#call` from now on has a special meaning in Ruby and probably decide on other APIs that should respect it (for example, auto-define `#to_proc` on any object that has `#call`)
Either is OK, the current situation is not.
PS: One more problem (that probably should be discussed separately) is that check for `#call` existence is performed pretty late, which can lead to this kind of errors:
```ruby
# At code loading time:
# I erroneously thought this is correct. It is not, but the line would perform without
# any error.
PROCESSOR = JSON.method(:parse) >> :symbolize_keys
# Later, in runtime:
'{"foo": "bar"}'.then(&PROCESSOR)
# NoMethodError (undefined method `call' for :symbolize_keys:Symbol)
```
**UPD 2018-12-29:** As this ticket was ignored prior to 2.6 release, I rewrote it in an "actionable" instead of "question" manner.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/