Continuation passing style vs. wrapper objects in dmd
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Tue May 25 17:21:16 UTC 2021
dmd has a few string functions with names having "Then" as a prefix that
take a lambda and call it with a temporary string converted for OS
purposes (zero-terminated, encoded a specific way etc). The use goes
like this:
int i = module_name.toCStringThen!(name => stat(name.ptr, &statbuf));
The way it goes, `module_name` gets converted from `char[]` to
null-terminated `char*`, the lambda gets invoked, then whatever
temporary memory allocated is freed just after the lambda returns.
I was thinking there's an easier way that's also more composable:
int i = stat(stringz(name).ptr, &statbuf));
where `stringz` returns a temporary struct offering primitives such as
`ptr` and `opSlice`. In the destructor, the struct frees temporary
memory if allocated. Better yet, it can return them as `scope` variable,
that way ensuring correctness in safe code.
Destruction of temporary objects has been sketchy in the past but I
assume things have been ironed out by now.
More information about the Digitalmars-d
mailing list