Thread overview
Nothing type/value aka Unit
Feb 08
Alex
Feb 08
Meta
6 days ago
dayllenger
Feb 08
JN
February 08
In D what would be the most appropriate type for a value that specifically does not carry a value?

I am working on porting Sodium, a simple FRP library, to D (or making one like it).  In FRP, events (aka event occurrences) must contain 1 and only 1 value.  So when there is no appropriate value, they make its value what they call a Unit.

For the java version of Sodium they define the unit like `enum Unit { UNIT }`.  In Haskell when this thing is needed `()` is used (an empty tuple).

Is there a more appropriate way to do this in D?  Perhaps in Phobos?

What makes more sense then `enum Unit { unit }` to me is `struct Unit {}; enum Unit unit;`.



February 08
Here is Sodium's GitHub page: https://github.com/SodiumFRP/sodium
February 08
On Friday, 8 February 2019 at 13:33:23 UTC, Jonathan Levi wrote:
> Here is Sodium's GitHub page: https://github.com/SodiumFRP/sodium

So... you want to return something, and void is not possible?

What about something like this:

´´´
import std.experimental.all;

void main() @nogc
{
    S!(typeof(null)) s;
    auto res1 = s.fun1;
    auto res2 = s.fun2;

    debug
    {
        writeln(res1);
        writeln(res2);
        writeln(unit);
    }
}

struct Unit {}
enum Unit unit = Unit.init;

struct S(T)
{
    T val;

    auto fun1()
    {
        return val;
    }

    auto fun2()
    {
        return tuple();
    }
}
´´´
February 08
On Friday, 8 February 2019 at 13:32:22 UTC, Jonathan Levi wrote:
> In D what would be the most appropriate type for a value that specifically does not carry a value?
>
> I am working on porting Sodium, a simple FRP library, to D (or making one like it).  In FRP, events (aka event occurrences) must contain 1 and only 1 value.  So when there is no appropriate value, they make its value what they call a Unit.
>
> For the java version of Sodium they define the unit like `enum Unit { UNIT }`.  In Haskell when this thing is needed `()` is used (an empty tuple).
>
> Is there a more appropriate way to do this in D?  Perhaps in Phobos?
>
> What makes more sense then `enum Unit { unit }` to me is `struct Unit {}; enum Unit unit;`.

There's no canonical unit type in D, so I'd recommend going with whichever fits best for your use case. A couple built-in ones would be an enum with a single value or a struct with no fields (as you already mentioned), as well as the `void` type (but D doesn't let you construct a value of type void), as well as `typeof(null)`, which has `null` as its only value.
February 08
On Friday, 8 February 2019 at 13:32:22 UTC, Jonathan Levi wrote:
> In D what would be the most appropriate type for a value that specifically does not carry a value?
>
> I am working on porting Sodium, a simple FRP library, to D (or making one like it).  In FRP, events (aka event occurrences) must contain 1 and only 1 value.  So when there is no appropriate value, they make its value what they call a Unit.
>
> For the java version of Sodium they define the unit like `enum Unit { UNIT }`.  In Haskell when this thing is needed `()` is used (an empty tuple).
>
> Is there a more appropriate way to do this in D?  Perhaps in Phobos?
>
> What makes more sense then `enum Unit { unit }` to me is `struct Unit {}; enum Unit unit;`.

how about Nullable!Event?
February 08
On Friday, 8 February 2019 at 14:41:16 UTC, JN wrote:
> how about Nullable!Event?

What is needed is more like Event!Unit.

Kinda like how `Unit[]` technically has values but they is meaningless; the length still is meaningful though.


February 08
On Friday, 8 February 2019 at 14:28:23 UTC, Meta wrote:
> `typeof(null)`, which has `null` as its only value.

Did not know that existed.

I think I will use:

    alias Unit = typeof(null);
    enum Unit unit = null;

Thanks!


February 09
On 08.02.19 14:32, Jonathan Levi wrote:
> In D what would be the most appropriate type for a value that specifically does not carry a value?

I usually use void[0].
6 days ago
On Friday, 8 February 2019 at 14:57:11 UTC, Jonathan Levi wrote:
> I think I will use:
>
>     alias Unit = typeof(null);
>     enum Unit unit = null;

void[0] is better, because it almost always has exactly 0 bytes size, but typeof(null) is 4-8 bytes depending of arch. Also there is a nasty segfault when trying to resize typeof(null) array, I don't know why, something related to unaligned data.