Continuation passing style vs. wrapper objects in dmd

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed May 26 03:01:02 UTC 2021


On 5/25/21 1:56 PM, Dennis wrote:
> On Tuesday, 25 May 2021 at 17:21:16 UTC, Andrei Alexandrescu wrote:
>> int i = stat(stringz(name).ptr, &statbuf));
> 
> I much prefer that form too, but unfortunately it only works with 
> immediate use. This is a big pitfall:
> ```D
> {
> const(char)* s = stringz(name).ptr;
> // destructor runs here
> printf(s); // use after free
> }
> ```
> 
> You have to write:
> ```D
> {
> auto temp = stringz(name);
> const(char)* s = temp.ptr;
> printf(s); // good!
> // destructor runs here
> }
> 
> ```

The CPS is exposed to the problem as well:

const char* p;
module_name.toCStringThen!(name => p = name.ptr);

> The first form should be preventable with -dip1000, but it isn't currently:
> https://issues.dlang.org/show_bug.cgi?id=20880
> https://issues.dlang.org/show_bug.cgi?id=21868

DIP1000 should help both, but the wrapper object is (to me at least) 
vastly easier on the eyes. Not to mention composability (what if you 
have two of those...)


More information about the Digitalmars-d mailing list