Current sentiment on Nullable.get

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Dec 10 17:43:40 UTC 2018


On Monday, December 10, 2018 3:01:45 AM MST FeepingCreature via Digitalmars-
d wrote:
> Having recently been reminded that `alias Nullable.get this`
> exists, I'm considering a pull request to deprecate it. What's
> the sentiment on `alias Nullable.get this` in the community? My
> unchanged stance is that it's a blight and an interminable source
> of impossible to find runtime bugs, and an anti-feature that
> misses the point of Nullable to provide safe optional types.
>
> The typical problem goes like this:
>
> 1. A dependency switches from T to Nullable!T.
> 2. You update your dependencies.
> 3. Your program still compiles (because Nullable!T silently casts
> to "T or exception") and you notice nothing.
> 4. Sometime later, your program crashes in production.
>
> Thoughts?

Personally, I don't think that it's all that big a deal, but I'm not really
against deprecating the alias either. Given that we don't have implicit
construction in D, the alias already fails at its goal of making a
Nullable!T work like a T (though even _with_ implicit construction, swapping
out a T for a Nullable!T would break code involving template constraints, so
implicit construction isn't the only problem there, but it's the most
obvious one). So, in general, I'm inclined to argue that it's just better
practice to use get explicitly, and I wouldn't mind it being deprecated.

However, IIRC, someone already tried to deprecate the alias and ran into a
compiler bug that prevented it - though unfortunately, I can't find the bug
report at the moment. I recall it being an issue with getting deprecation
messages from the alias even when it wasn't used explicitly, but when I try
a simple test case, I get a different bug entirely. This code snippet

struct S
{
    int get()
    {
        return 42;
    }

    deprecated alias get this;
}

void main()
{
    S s;
    int i = s;
}

doesn't result in any deprecation messages at all. Based on that, without
some compiler fixes, it's not actually possible to deprecate the alias.

- Jonathan M Davis





More information about the Digitalmars-d mailing list