www.digitalmars.com         C & C++   DMDScript  

digitalmars.dip.ideas - Allow expressions involving preceding function parameters in parameter

reply Quirin Schroll <qs.il.paperinik gmail.com> writes:
Essentially, you should be able to write `void f(int x, int y = 
x)` with the idea that if you omit `y`, it’s equal to whatever 
you passed to `x`. It would allow arbitrary expressions, e.g. `x 
+ 1`.

Technically, this would be a breaking change because if `x` in 
`int y = x` can refer to something else. Therefore, this would be 
for the next Edition.

 From a technical standpoint, initializing function arguments is 
no different than initializing local variables, except they’re in 
a different stack frame, and local variables can be initialized 
with expressions using earlier local variables.
Apr 10
next sibling parent reply Dom Disc <dominikus scherkl.de> writes:
On Friday, 10 April 2026 at 14:41:52 UTC, Quirin Schroll wrote:
 Essentially, you should be able to write `void f(int x, int y = 
 x)` with the idea that if you omit `y`, it’s equal to whatever 
 you passed to `x`. It would allow arbitrary expressions, e.g. 
 `x + 1`.

 Technically, this would be a breaking change because if `x` in 
 `int y = x` can refer to something else. Therefore, this would 
 be for the next Edition.

 From a technical standpoint, initializing function arguments is 
 no different than initializing local variables, except they’re 
 in a different stack frame, and local variables can be 
 initialized with expressions using earlier local variables.
At the moment you can do ```d void f(int x, int y = 0) { if(!y) y = x; ... } ``` So I see no reason to allow this.
Apr 11
parent Nick Treleaven <nick geany.org> writes:
On Saturday, 11 April 2026 at 09:14:25 UTC, Dom Disc wrote:
 At the moment you can do
 ```d
 void f(int x, int y = 0)
 {
    if(!y) y = x;
    ...
 }
 ```
 So I see no reason to allow this.
That breaks when `f` is called as `f(arg, 0)`.
Apr 11
prev sibling next sibling parent reply Nick Treleaven <nick geany.org> writes:
On Friday, 10 April 2026 at 14:41:52 UTC, Quirin Schroll wrote:
 Essentially, you should be able to write `void f(int x, int y = 
 x)` with the idea that if you omit `y`, it’s equal to whatever 
 you passed to `x`. It would allow arbitrary expressions, e.g. 
 `x + 1`.

 Technically, this would be a breaking change because if `x` in 
 `int y = x` can refer to something else. Therefore, this would 
 be for the next Edition.
People are going to want to update their code to newer editions, and we should avoid silent behaviour change. But we could have an error if `x` is in scope before the function declaration and there is a parameter `x` declared before `y`.
Apr 11
parent Quirin Schroll <qs.il.paperinik gmail.com> writes:
On Saturday, 11 April 2026 at 10:04:47 UTC, Nick Treleaven wrote:
 On Friday, 10 April 2026 at 14:41:52 UTC, Quirin Schroll wrote:
 Essentially, you should be able to write `void f(int x, int y 
 = x)` with the idea that if you omit `y`, it’s equal to 
 whatever you passed to `x`. It would allow arbitrary 
 expressions, e.g. `x + 1`.

 Technically, this would be a breaking change because if `x` in 
 `int y = x` can refer to something else. Therefore, this would 
 be for the next Edition.
People are going to want to update their code to newer editions, and we should avoid silent behaviour change. But we could have an error if `x` is in scope before the function declaration and there is a parameter `x` declared before `y`.
Alternatively make it a deprecation.
Apr 27
prev sibling parent reply Dukc <ajieskola gmail.com> writes:
On Friday, 10 April 2026 at 14:41:52 UTC, Quirin Schroll wrote:
 Essentially, you should be able to write `void f(int x, int y = 
 x)` with the idea that if you omit `y`, it’s equal to whatever 
 you passed to `x`. It would allow arbitrary expressions, e.g. 
 `x + 1`.
Good idea probably, but the question is how to make [ParameterDefaults](https://github.com/dlang/phobos/blob/v2.112.1 std/traits.d#L1400) work again. It can't just return a sequence of values anymore. Also, as you can see, `ParameterDefaults` already works only thanks to highly advanced metaprogramming. Do we need (or want) a new language-level trait to implement the new alternative, or can more of the clever Phobos metaprogramming handle it? Myself I don't understand templates and traits well enough to answer this straight away.
 Technically, this would be a breaking change because if `x` in 
 `int y = x` can refer to something else. Therefore, this would 
 be for the next Edition.
Maybe. OTOH I expect such a problem to be rare enough that we could maybe look at the edition only in case of actual ambiguity, with a warning message for the old edition.
Apr 14
parent Quirin Schroll <qs.il.paperinik gmail.com> writes:
On Tuesday, 14 April 2026 at 11:56:40 UTC, Dukc wrote:
 On Friday, 10 April 2026 at 14:41:52 UTC, Quirin Schroll wrote:
 Essentially, you should be able to write `void f(int x, int y 
 = x)` with the idea that if you omit `y`, it’s equal to 
 whatever you passed to `x`. It would allow arbitrary 
 expressions, e.g. `x + 1`.
Good idea probably, but the question is how to make [ParameterDefaults](https://github.com/dlang/phobos/blob/v2.112.1 std/traits.d#L1400) work again. It can't just return a sequence of values anymore. Also, as you can see, `ParameterDefaults` already works only thanks to highly advanced metaprogramming. Do we need (or want) a new language-level trait to implement the new alternative, or can more of the clever Phobos metaprogramming handle it? Myself I don't understand templates and traits well enough to answer this straight away.
 Technically, this would be a breaking change because if `x` in 
 `int y = x` can refer to something else. Therefore, this would 
 be for the next Edition.
Maybe. OTOH I expect such a problem to be rare enough that we could maybe look at the edition only in case of actual ambiguity, with a warning message for the old edition.
It probably can’t work for those and would become a best-effort kind of thing, i.e. it gives you defaults if the values are compile-time constants and `void` if they’re not given at compile-time (which covers both run-time-default and no-default parameters).
Apr 27