digitalmars.dip.ideas - Allow expressions involving preceding function parameters in parameter
- Quirin Schroll (11/11) Apr 10 Essentially, you should be able to write `void f(int x, int y =
- Dom Disc (10/21) Apr 11 At the moment you can do
- Nick Treleaven (2/11) Apr 11 That breaks when `f` is called as `f(arg, 0)`.
- Nick Treleaven (5/12) Apr 11 People are going to want to update their code to newer editions,
- Quirin Schroll (2/15) Apr 27 Alternatively make it a deprecation.
- Dukc (12/19) Apr 14 Good idea probably, but the question is how to make
- Quirin Schroll (6/26) Apr 27 It probably can’t work for those and would become a best-effort
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
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
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
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
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:Alternatively make it a deprecation.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 27
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
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: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).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 27









Nick Treleaven <nick geany.org> 