Truly algebraic Variant and Nullable

Petar Petar
Sun Dec 20 12:32:35 UTC 2020


On Sunday, 15 November 2020 at 04:54:19 UTC, 9il wrote:
> Truly algebraic Variant and Nullable with an order-independent 
> list of types.
>
> Nullable is defined as
> ```
> alias Nullable(T...) = Variant!(typeof(null), T);
> ```
>
> Variant and Nullable with zero types are allowed.
>
> `void` type is supported.
>
> Visitors are allowed to return different types.
>
> Cyclic referencing between different variant types are 
> supported.
>
> More features and API:
>
> http://mir-core.libmir.org/mir_algebraic.html
>
> Cheers,
> Ilya
>
> The work has been sponsored by Kaleidic Associates and Symmetry 
> Investments.

I have been using SumType [1] for a while in some of my projects 
and I'm quite happy with it. The author has been very responsive 
to feedback and the quality bar of his work is definitely higher 
than that of many other D libraries (e.g. support for 
@safe/pure/@nogc/nothrow, immutable, betterC and DIP1000, etc.).

That said, I'm also a fan of your work with Mir! mir.algorithm 
(which I'm most familiar with) is a text book example of 
high-quality generic algorithm design.

How does your work compare to sumtype? Would mir.algebraic offer 
any benefits, which would make it worth switching over?

IMO, algebraic types (sum and tuple types) should be a core 
language feature part of druntime and should have have 
corresponding syntax sugar:

// Same as Tuple!(T, "open", T, "high", T, "low", T, "close"):
alias OhlcTuple(T) = (T open, T high, T low, T close);

// Same as:
// Union!(long, double, bool, typeof(null), string,
//     This[], This[string];
alias Json =
     | long
     | double
     | bool
     | typeof(null)
     | string
     | Json[]
     | Json[string];

// Syntax sugar for nullable/optional types -
// T? == Nullable!T == Union!(typeof(null), T):
alias ResponseParser = OhlcTuple!double? delegate(Json json);

If we can work together to consolidate on a single API, I think 
it would be better for the language ecosystem.

[1]: https://code.dlang.org/packages/sumtype


More information about the Digitalmars-d-announce mailing list