Jump to page: 1 2
Thread overview
August 24

On page 549 of Programming in D, it appears that D supports 'escaping' local variables to the heap, when returning their address.

This is similar to Go.
Is this what is actually going on?
Is this 'safe' to do?

&result: AC067AF730
&result: AC067AF730
ptrSum : AC067AF7B0 (2 + 3)
sum    : (4 * 5)

source/app.d

import std.stdio;

void main() {
    string * ptrSum = &parenthesized("2 + 3");
	string sum = parenthesized("4 * 5");

    writefln("ptrSum : %s %s", ptrSum, *ptrSum);
    writefln("sum    : %s", sum);
}

auto ref string parenthesized(string phrase) {
    string result = '(' ~ phrase ~ ')';
	writeln("&result: ", &result);
    return result;      // ← compilation ERROR
}
August 24

On Sunday, 24 August 2025 at 15:03:12 UTC, Brother Bill wrote:

>

On page 549 of Programming in D, it appears that D supports 'escaping' local variables to the heap, when returning their address.

This is similar to Go.
Is this what is actually going on?
Is this 'safe' to do?

&result: AC067AF730
&result: AC067AF730
ptrSum : AC067AF7B0 (2 + 3)
sum    : (4 * 5)

source/app.d

import std.stdio;

void main() {
    string * ptrSum = &parenthesized("2 + 3");
	string sum = parenthesized("4 * 5");

    writefln("ptrSum : %s %s", ptrSum, *ptrSum);
    writefln("sum    : %s", sum);
}

auto ref string parenthesized(string phrase) {
    string result = '(' ~ phrase ~ ')';
	writeln("&result: ", &result);
    return result;      // ← compilation ERROR
}

I don't know what's happening here, but it seems like a bug. This should not compile (it does for me, despite the comment above).

If you change auto ref to just ref, it fails, and if you change it to just returning string it fails.

If you change it to returning auto ref without string, it also fails.

It appears that the address taken is one of a local stack temporary.

-Steve

August 24

On Sunday, 24 August 2025 at 15:29:01 UTC, Steven Schveighoffer wrote:

>

I don't know what's happening here, but it seems like a bug. This should not compile (it does for me, despite the comment above).

-Steve

It does compile. I didn't change the comment from earlier code.

The core question is whether this is clean, safe D code.

August 24

On Sunday, 24 August 2025 at 15:47:28 UTC, Brother Bill wrote:

>

On Sunday, 24 August 2025 at 15:29:01 UTC, Steven Schveighoffer wrote:

>

I don't know what's happening here, but it seems like a bug. This should not compile (it does for me, despite the comment above).

It does compile. I didn't change the comment from earlier code.

The core question is whether this is clean, safe D code.

No, it's code that shouldn't compile IMO. Don't count on this working in a future version.

-Steve

August 24
On 8/24/25 8:03 AM, Brother Bill wrote:
> auto ref string parenthesized(string phrase) {
>      string result = '(' ~ phrase ~ ')';
>      writeln("&result: ", &result);
>      return result;      // ← compilation ERROR
> }

First, thank you for reading the book and raising so many issues. Very much appreciated!

In this case, you used the body of a 'ref' function but compiled it as 'auto ref'. Please remove 'auto' above.

Ali

August 24
On Sunday, 24 August 2025 at 17:30:33 UTC, Ali Çehreli wrote:
> In this case, you used the body of a 'ref' function but compiled it as 'auto ref'. Please remove 'auto' above.
>
> Ali

The book has 'auto ref' in "auto ref functions" section.
Does your book need a change?
I'm not sure why we should remove 'auto' as you suggest.

It would be helpful if you would provide a short toy program that has the changes you seek.

August 24
On Sunday, 24 August 2025 at 17:30:33 UTC, Ali Çehreli wrote:
> First, thank you for reading the book and raising so many issues. Very much appreciated!

> Ali

Ali, may I have a Yes or No on permission to use your Programming in D examples that I am expanding for a commercial Udemy course that I will build.  Of course, I will provide any Copyright notice that you provide, if permission is granted.

Once I complete the remaining 200 pages in your book, I'll polish and release them for free public usage.  This should assist D newbies in mastering D language.

FWIW, the most difficult area to learn in D is definitely the 'template' feature.

I have Asperger's syndrome, which is on the Autism spectrum.

I need working code to learn from.  So many authors assume that their readers can connect the dots, and fill in the gaps.  I cannot. But with the cost of text "too cheap to meter', it would be good to have a book (even digital pdf) have links to 'full working code' in the cloud.  These links could be updated as the D language 'mutates'.

Rather than curse the 'darkness', I have lit a 'candle', which will be your examples fleshed out into working examples.  Perhaps every 10 - 15 years is a good time to catch up with changes to D language.

Also, would now be a good time to revise "Programming in D" to 'Second Edition'?

August 24
On 8/24/25 11:29 AM, Brother Bill wrote:

> Ali, may I have a Yes or No on permission to use your Programming in D
> examples that I am expanding for a commercial Udemy course that I will
> build.

If this forum post is authoritative for you, yes, I give permission.

> Also, would now be a good time to revise "Programming in D" to 'Second
> Edition'?

It's been very long since an update was needed. Unfortunately, I don't have enough motivation yet. I still see motivation as magic; I don't know where it comes from.

Ali

August 24
On 8/24/25 11:17 AM, Brother Bill wrote:
> On Sunday, 24 August 2025 at 17:30:33 UTC, Ali Çehreli wrote:
>> In this case, you used the body of a 'ref' function but compiled it as
>> 'auto ref'. Please remove 'auto' above.
>>
>> Ali
>
> The book has 'auto ref' in "auto ref functions" section.

Yes.

> Does your book need a change?

Not in this case.

> I'm not sure why we should remove 'auto' as you suggest.

The function you show is from this section:

 https://ddili.org/ders/d.en/functions_more.html#ix_functions_more.ref,%20return%20type

Note that the function body that you used, the one which has "// ← compilation ERROR" does not return 'auto ref', but 'ref':

ref string parenthesized(string phrase) {
    string result = '(' ~ phrase ~ ')';
    return result;    // ← compilation ERROR
} // ← the lifetime of result ends here

If you use the function above in your example, meaning, if you remove 'auto' from the code you've shown here, you will get the compilation error.

So, the example in the book is correct.

I think you went a little further in that section and made the return 'auto ref', which does not (and should not) fail compilation.

'ref' functions are different from 'auto ref' functions.

>
> It would be helpful if you would provide a short toy program that has
> the changes you seek.
>

I hope I was clear enough. Start with the program you've shown and simply remove 'auto' from the function return type. The return type should be just 'ref'.

Ali

August 24

On Sunday, 24 August 2025 at 15:29:01 UTC, Steven Schveighoffer wrote:

> >
import std.stdio;

void main() {
    string * ptrSum = &parenthesized("2 + 3");
	string sum = parenthesized("4 * 5");

    writefln("ptrSum : %s %s", ptrSum, *ptrSum);
    writefln("sum    : %s", sum);
}

auto ref string parenthesized(string phrase) {
    string result = '(' ~ phrase ~ ')';
	writeln("&result: ", &result);
    return result;      // ← compilation ERROR
}

I don't know what's happening here, but it seems like a bug. This should not compile (it does for me, despite the comment above).

Yes, I think it's https://github.com/dlang/dmd/issues/19893. If parenthesized is moved above main, I get:

Error: cannot take address of expression `parenthesized("2 + 3")` because it is not an lvalue
    string * ptrSum = &parenthesized("2 + 3");
                                    ^
« First   ‹ Prev
1 2