CARVIEW |
Select Language
HTTP/2 200
x-amz-id-2: hCxnMzb0MwBiUIy2CNb+dT2ywMdRaZcTrl+ATF+n1TeIfNNRwNB6CaBR9jYmM4IVJxUvolEO7jvPKtinBdL8EfiRj9fvISfWwpVw9Vi4+vc=
x-amz-request-id: 7M5FHFG8K5SJ8P61
last-modified: Wed, 16 Jul 2025 15:02:17 GMT
etag: "1e35eee07243954f7223d1f3497e12d3"
x-amz-server-side-encryption: AES256
server: AmazonS3
content-encoding: gzip
via: 1.1 varnish, 1.1 varnish
content-type: text/plain; charset=utf-8
accept-ranges: bytes
date: Wed, 23 Jul 2025 20:33:19 GMT
age: 0
x-served-by: cache-tyo11965-TYO, cache-bom-vanm7210053-BOM
x-cache: MISS, MISS
x-cache-hits: 0, 0
x-timer: S1753302799.476298,VS0,VE432
vary: Accept-Encoding
content-length: 1167
From: "nhorton (Noah Horton) via ruby-core"
Date: 2025-07-16T14:56:50+00:00
Subject: [ruby-core:122797] [Ruby Feature#21515] Add `&return` as sugar for `x=my_calculation; return x if x`
Issue #21515 has been reported by nhorton (Noah Horton).
----------------------------------------
Feature #21515: Add `&return` as sugar for `x=my_calculation; return x if x`
https://bugs.ruby-lang.org/issues/21515
* Author: nhorton (Noah Horton)
* Status: Open
----------------------------------------
Let me preface this by saying I have no marriage to the exact keyword name of `&return`.
# Problem
It is very common to have an early return in code where you get some initial value and return it if it is non-null. i.e.
```
return my_calculation(input_a, input_b) if my_calculation(input_a, input_b)
```
That form on its own is verbose and one where you need to look at it for a moment to confirm it is the same code on either side of the if.
If `my_calculation` is non-trivial at all, it normally gets turned into something with a variable:
```
my_calc = my_calculation(input_a, input_b)
return my_calc if my_calc
```
That is now two lines. The worse scenario, however, is if the user did not bother doing that and my_calculation turned out to be expensive (and they did not know it).
# Proposal
I propose a syntax of `&return my_calculation(input_a, input_b)` where it will evaluate the argument and return it as the result of the method if it is non-nil, otherwise it will continue on.
# Alternatives
## Do Nothing
There is no way to work around this with rolling your own methods. You can't make a `returnif` method or something yourself since you can't do a return in the caller's scope.
## Different Name
The best other name I saw were permutations of `returnif`. The biggest issue I see is the similarity of the following two statements:
```
return if foo
returnif foo
```
Those are obviously very, very different statements, but are somewhat similar. However, things like this are common, and the code is still quite distinct on those, so I think it is acceptable.
Ultimately, this feels so similar to the safe navigator that using `&` in this context feels appropriate.
Thank you all for your consideration.
--
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/lists/ruby-core.ml.ruby-lang.org/