[Issue 14477] Nullable does not work with structs with default constructor disabled

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Jul 25 00:25:29 PDT 2016


https://issues.dlang.org/show_bug.cgi?id=14477

--- Comment #3 from MichaelZ <dlang.org at bregalad.de> ---
This is currently an issue for us, and it has been proposed we use
Algebraic!Foo instead, which appears to work sufficiently, see below.

What aspects are against doing it this way?
The implementation of Algebraic "looks" a lot more heavyweight, but is it
really, particularly in this very specific usage?
If we don't like the toString behaviour, or want to have .nullify, and prefer
.isNull to .hasValue, we can always make Nullable a pretty minimal wrapper...

Thoughts?

-----

import std.variant;
import std.stdio;
import std.string;

struct Foo
{
    int x;
    @disable this();
    this(int value) { x=value; }
}

void main()
{
     Algebraic!Foo foo;

     writeln("hasValue for default-initialized foo: %s".format(foo.hasValue));
     writeln("Format default-initialized foo: %s".format(foo));

     foo = Foo(5);

     writeln("hasValue for set foo: %s".format(foo.hasValue));
     writeln("Format set foo: %s".format(foo));

     foo = Algebraic!Foo();  // Nullable's foo.nullify is cooler :/

     writeln("hasValue for reset foo: %s".format(foo.hasValue));
     writeln("Format reset foo: %s".format(foo));
}

--- output ---

hasValue for unset foo: false
Format unset foo: <Uninitialized VariantN>
hasValue for set foo: true
Format set foo: Foo(5)
hasValue for unset foo: false
Format unset foo: <Uninitialized VariantN>

--


More information about the Digitalmars-d-bugs mailing list