From: bughitgithub@... Date: 2019-11-01T21:33:23+00:00 Subject: [ruby-core:95644] [Ruby master Feature#11660] a falsy value (similar to js undefined) that facilitates forwarding of default arguments Issue #11660 has been updated by bughit (bug hit). > Other than arity, it seems like you would want to use the new argument forwarding syntax I didn't know about new syntax, is that in 2.7? However, I don't think that's general enough. In javascript you can forward individual arguments to an arbitrary depth and the default value is computed once at the end. ```js function f1(a = defaultValueExpression) { } function f2({b = 1, c} = {}) { f1(c); } function f3(d, e) { f2({b: d, c: e}); } f3(1) // or f3(1, undefined) ``` the missing/undefined second arg to f3 invocation flows all the way down to f1 where it triggers default value computation You can't do this in ruby, to accomplish the equivalent you'd have to stop using intended default value expressions, forward nils, and then compute the default value in the body of the method. ```ruby def f1(a = nil) a = default_value_expression if a.nil? end def f2(b: 1, c: nil) f1(c) end def f3(d, e = nil) f2(b: d, c: e) end f3(1) ``` So in ruby the default value feature is close to being demoware. In practice, when composing methods, you end up reverting to the above. > The major issue with adding this support is what the value of this is The value would be `missing`. Adding something like `missing` to the language would need to be coupled with a null coalescing operator which would cover both nil and missing (but not false). Something like `??` and `??=` ---------------------------------------- Feature #11660: a falsy value (similar to js undefined) that facilitates forwarding of default arguments https://bugs.ruby-lang.org/issues/11660#change-82432 * Author: bughit (bug hit) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- I'll call it "missing" here. Consider the following scenario: ```ruby def foo(default1 = some_expression) end def bar(default1 = some_expression) foo default1 end def foobar(default1 = some_expression) bar default1 end ``` if you had "missing": ```ruby def foo(default1 = some_expression) end def bar(default1 = missing) foo default1 end def foobar(default1 = missing) bar default1 end ``` missing passed as arg would be ignored (as if it wasn't passed at all) and you wouldn't have to repeat the default value expression in every method I believe that's how undefined works in js6 with respect to default args -- https://bugs.ruby-lang.org/ Unsubscribe: