Flaw in DIP1000? Returning a Result Struct in DIP1000
Jack Stouffer
jack at jackstouffer.com
Wed Mar 21 17:13:40 UTC 2018
Consider this example simplified from this PR
https://github.com/dlang/phobos/pull/6281
------
struct GetoptResult
{
Option[] options;
}
struct Option
{
string optShort;
string help;
}
GetoptResult getopt(T...)(scope T opts) @safe
{
GetoptResult res;
auto o = Option(opts[0], opts[1]);
res.options ~= o;
return res;
}
void main() @safe
{
bool arg;
getopt("arg", "info", &arg);
}
------
$ dmd -dip1000 -run main.d
------
main.d(16): Error: scope variable o assigned to non-scope res
main.d(23): Error: template instance `onlineapp.getopt!(string,
string, bool*)` error instantiating
------
The only way I've found to make the code compile and retain the
pre-dip1000 behavior is to change the Option construction to
------
auto o = Option(opts[0].idup, opts[1].idup);
------
How can we return non-scoped result variables constructed from
scope variables without copies?
More information about the Digitalmars-d
mailing list