D Lang'99 & Its future

bauss jj_1337 at live.dk
Wed Feb 9 12:47:34 UTC 2022


On Wednesday, 9 February 2022 at 12:24:32 UTC, Martin 
Tschierschke wrote:
> On Wednesday, 9 February 2022 at 10:21:33 UTC, bauss wrote:
>> On Wednesday, 9 February 2022 at 08:38:00 UTC, Martin 
>> Tschierschke wrote:
>>> On Tuesday, 8 February 2022 at 18:28:21 UTC, H. S. Teoh wrote:
>>>
>>>>
>>>> In latest dmd git HEAD, you need to annotate this function 
>>>> with `return`, because it escapes a reference to `this`. :-)
>>>>
>>> Sorry, I am to stupid, where do you have to "annotate return"?
>>
>> I believe it would be like this:
>>
>> ```d
>> ref string toString() return
>> {
>>   return bits;
>> }
>> ```
> Thank you I tried it, at the https://tour.dlang.org/ rdmd 
> Playground and
> got a casting error, so I changed ```string``` to ```char[8]``` 
> and it worked:
> no return needed: (?)
> ```
> import std.stdio;
> void main()
> {
>  enum Dlang99 = 4026728633831507;
>
>   union Magic
>   {
>     long data;
>     char[8] bits;
>     char[8] toString()
>     {
>       return bits;
>     }
>   }
>   auto magic = Magic(Dlang99<<10|'D');
>        magic.writefln!"..::%s::..";
> }
>
> ```

toString() should return a string, so what should happen is that 
the value of bits must actually be converted to string.

The error is evident if you ex. try to cast bits to string like 
this:

```
     string toString()
     {
       return cast(string)bits;
     }
```

Which will give this error:

```
onlineapp.d(12): Deprecation: returning `cast(string)this.bits` 
escapes a reference to parameter `this`
onlineapp.d(12):        perhaps annotate the parameter with 
`return`
```

So when we do:

```
     string toString() return
     {
       return cast(string)bits;
     }
```

There's no error.

Probably a terrible example and I'm not sure if casting like that 
is even considered safe or should even be done like that, but 
yeah...


More information about the Digitalmars-d mailing list