July 22

Re: Inlined out arguments declaration
Gravatar of JurajPosted by Juraj
in reply to Juraj
PermalinkReply
Gravatar of Juraj

Posted in reply to Juraj

FIXED:

int* ptr1;
int* ptr2;
if (foo(ptr1) || bar(ptr2)) {
    int val1 = *ptr1;
    int val2 = *ptr2;
}

bool foo(out int* p) {
    p = null;
    return true;
}

bool bar(out int* p) {
  p = null;
  return true;
}

July 22

Re: Inlined out arguments declaration
Gravatar of user1234Posted by user1234
in reply to Juraj
PermalinkReply

user1234

Gravatar of user1234

Posted in reply to Juraj

On Wednesday, 22 July 2026 at 08:25:49 UTC, Juraj wrote:

>

On Tuesday, 21 July 2026 at 21:20:53 UTC, Richard (Rikki) Andrew Cattermole wrote:

>

Consider:

if (foo(int* ptr1) || bar(int* ptr2)) {
	int val1 = *ptr1;
	int val2 = *ptr2;
}

You can't prove that this will not dereference null, they are mutually exclusive.

And before you say, oh we can disallow logical or expressions! Nope.

This is legal D code, not sure it is related to the proposed inline declaration.

int* ptr1;
int* ptr2;
if (foo(int* ptr1) || bar(int* ptr2)) {
    int val1 = *ptr1;
    int val2 = *ptr2;
}

bool foo(out int* p) {
    p = null;
    return true;
}

bool boo(out int* p) {
    p = null;
    return true;
}

>

Oh and btw the name of the pattern that C# uses here is called TryGet.

And I use this pattern in D daily, the only issue I have is the scope of the out vars.

"out vars" is a limited point of view. Imagine a variable declaration as an expression. Everything is more simple.

You see, this is something I really worked on; I still believe this would be nice for D. Problem is the way we think default init. Styx is "zeroes", D is "poison you can track".

Now about the scope problem. There's no problem. You can test the idea with styx.

July 22

Re: Inlined out arguments declaration
Gravatar of Richard (Rikki) Andrew CattermolePosted by Richard (Rikki) Andrew Cattermole
in reply to Juraj
PermalinkReply

Richard (Rikki) Andrew Cattermole

Gravatar of Richard (Rikki) Andrew Cattermole

Posted in reply to Juraj

On 22/07/2026 8:25 PM, Juraj wrote:
> This is legal D code, not sure it is related to the proposed /inline declaration/.

You have misunderstood the purpose of this syntax, and its implications.

The TryGet pattern is unlikely to result in a null value if it returns true.

In 2019 C# introduced attributes as part of the compilers static analysis capabilities for nullability specifically to solve this.

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/nullable-analysis#conditional-postconditions-notnullwhen-maybenullwhen-and-notnullifnotnull

```c#
bool TryGetMessage(string key, [NotNullWhen(true)] out string? message)
{
    if (_messageMap.ContainsKey(key))
        message = _messageMap[key];
    else
        message = null;
    return message is not null;
}
```

Being legal, and being correct code is two vastly different things.

This particular feature being proposed has limited uses outside of the TryGet pattern, so its correctness and program security must be considered and weighted quite heavily compared to other use cases.

July 22

Re: Inlined out arguments declaration
Gravatar of Anton PastukhovPosted by Anton Pastukhov
in reply to Richard (Rikki) Andrew Cattermole
PermalinkReply

Anton Pastukhov

Gravatar of Anton Pastukhov

Posted in reply to Richard (Rikki) Andrew Cattermole

On Wednesday, 22 July 2026 at 09:28:59 UTC, Richard (Rikki) Andrew Cattermole wrote:
> This particular feature being proposed has limited uses outside of the TryGet pattern.

The limited scope of the proposal is intentional. I believe that being a small, low-risk change is the only possible way for it to see the light of day. user1234's idea, as interesting as it is, is unlikely to happen in D.

This is really just a minor syntax sugar/ergonomic improvement. In 2026 D feels old-school and boxy sometimes.

July 22

Re: Inlined out arguments declaration
Gravatar of JurajPosted by Juraj
in reply to Richard (Rikki) Andrew Cattermole
PermalinkReply

Juraj

Gravatar of Juraj

Posted in reply to Richard (Rikki) Andrew Cattermole

On Wednesday, 22 July 2026 at 09:28:59 UTC, Richard (Rikki) Andrew Cattermole wrote:

>

You have misunderstood the purpose of this syntax, and its implications.

I am saying, you are dismissing this idea on a premise, that something already allowed in the language is problematic.
So I do not see how that as good argument.

July 22

Re: Inlined out arguments declaration
Gravatar of Richard (Rikki) Andrew CattermolePosted by Richard (Rikki) Andrew Cattermole
in reply to Anton Pastukhov
PermalinkReply

Richard (Rikki) Andrew Cattermole

Gravatar of Richard (Rikki) Andrew Cattermole

Posted in reply to Anton Pastukhov

On 22/07/2026 10:10 PM, Anton Pastukhov wrote:
> On Wednesday, 22 July 2026 at 09:28:59 UTC, Richard (Rikki) Andrew Cattermole wrote:
>> This particular feature being proposed has limited uses outside of the TryGet pattern.
> 
> The limited scope of the proposal is intentional. I believe that being a small, low-risk change is the only possible way for it to see the light of day. user1234's idea, as interesting as it is, is unlikely to happen in D.

The limited scope of this proposal isn't an issue.

Its not far off from what I'd do, if I was to pursue it.

See my comment a bit later on in that message: "This particular feature being proposed has limited uses outside of the TryGet pattern, so its correctness and program security must be considered and weighted quite heavily compared to other use cases."

> This is really just a minor syntax sugar/ergonomic improvement. In 2026 D feels old-school and boxy sometimes.

When Nic introduced variable declaration support to with statements, it broke code.

This could be a lot worse depending upon implementation and exact grammar changes. The potential here is for it to infect non-function-argument expressions.

July 22

Re: Inlined out arguments declaration
Gravatar of Nick TreleavenPosted by Nick Treleaven
in reply to Juraj
PermalinkReply

Nick Treleaven

Gravatar of Nick Treleaven

Posted in reply to Juraj

On Wednesday, 22 July 2026 at 10:19:26 UTC, Juraj wrote:

>

I am saying, you are dismissing this idea on a premise, that something already allowed in the language is problematic.
So I do not see how that as good argument.

That is backwards - adding special language support for something that is problematic is by definition problematic.

July 22

Re: Inlined out arguments declaration
Gravatar of Richard (Rikki) Andrew CattermolePosted by Richard (Rikki) Andrew Cattermole
in reply to Juraj
PermalinkReply

Richard (Rikki) Andrew Cattermole

Gravatar of Richard (Rikki) Andrew Cattermole

Posted in reply to Juraj

On 22/07/2026 10:19 PM, Juraj wrote:
> On Wednesday, 22 July 2026 at 09:28:59 UTC, Richard (Rikki) Andrew Cattermole wrote:
> 
>> You have misunderstood the purpose of this syntax, and its implications.
> 
> 
> I am saying, you are dismissing this idea on a premise, that something already allowed in the language is problematic.
> So I do not see how that as good argument.

I have not dismissed this based upon what is already existing in the language.

I have dismissed it due to:

1. Interaction with other language features.

2. Reviewing how it is used in C#, which to my knowledge is the only mainstream language & ecosystem example of this.

3. How this would be utilized in PhobosV3, given the C# influence and what classes of bugs we would be recommending people to adopt (which is something both me and Adam want to avoid, if not outright disallow).

4. Introduces new failure cases that require static analysis which I would be the one having to implement AND that static analysis requires attributes to model that people have to use AND there is a much simpler alternative that entirely eliminates that class of bug AND this other feature has been used in multiple mainstream languages & ecosystems without any problems AND this other features doesn't need any kind of static analysis.

Workability of a new language feature is only the starting point, there are other concerns like fallibility, program security and if its modellable that must be considered especially if that feature is a quality of life one.

I've had years to study this particular set of problems, hence my strong opinions.

I do understand why this feature looks attractive.

July 22

Re: Inlined out arguments declaration
Gravatar of JurajPosted by Juraj
in reply to Richard (Rikki) Andrew Cattermole
PermalinkReply

Juraj

Gravatar of Juraj

Posted in reply to Richard (Rikki) Andrew Cattermole

On Wednesday, 22 July 2026 at 11:19:07 UTC, Richard (Rikki) Andrew Cattermole wrote:

>

  1. How this would be utilized in PhobosV3, given the C# influence and what classes of bugs we would be recommending people to adopt (which is something both me and Adam want to avoid, if not outright disallow).

Fair and good luck

>

  1. Introduces new failure cases that require static analysis which I would be the one having to implement AND that static analysis requires attributes to model that people have to use AND there is a much simpler alternative that entirely eliminates that class of bug AND this other feature has been used in multiple mainstream languages & ecosystems without any problems AND this other features doesn't need any kind of static analysis.

This is the thing that eludes me, what new failure cases?
I can do all the problematic parts right now in D, they do not emerge from this proposal.

Just to clarify my position.
I would love to be able to limit the "scope" of vars in if statements, but I acknowledge all the shortcomings.
And based of my POC implementation I myself would no go further with this.
D and C# are my main languages (both hobby and professionally) and to my own surprise I dont use inlined declarations in C# at all ;-)

September 17

Re: Inlined out arguments declaration
Gravatar of Quirin SchrollPosted by Quirin Schroll
in reply to Juraj
PermalinkReply

Quirin Schroll

Gravatar of Quirin Schroll

Posted in reply to Juraj

On Tuesday, 21 July 2026 at 17:56:35 UTC, Juraj wrote:

>

On Tuesday, 21 July 2026 at 17:44:48 UTC, Anton Pastukhov wrote:

>

Thanks, and I'm glad I'm not the only one bothered with the existing implementation.
The problem with destructors both you and Rikki Cattermole mentioned honestly eludes me.

void foo() {
    if (Boo(out int x, out float y)) {
        ...
    }
}

should be functionally identical to

void foo() {
    int x;
    int y;

    if (Boo(x, y)) {
        ...
    }
}

...which is perfectly fine D code. There's no behavior change, including the behavior of destructors.

Basically having the x and y be in the scope of foo and not only the if(true) branch diminish a lot of the value, at that point, just write it out.

Please don’t. This is annoying in C#. The declared variables have way too much scope. If the call is verbatim in a scope, put them there. But if they’re on an if, only make them valid in the if―else, not outside, so that another if can re-use the variable name.

Example:

void main()
{
    if(foo(out int x)) { }
    x = 0; // error, no `x` in scope, it’s for `if` only

    foo(out int y);
    y = 1; // okay

    // compare:

    if (auto z = bar()) { }
    z = 0; // error, no `z` in scope
}

Effectively, one can wrap any if/while/etc. in {}: if(…) {…} else {…} is equivalent to {if(…) {…} else {…}}. If we allow declaring out variables, I want them injected in this implicit scope, not outside. That also answers when destructors ought to run.