www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Nothing type/value aka Unit

reply Jonathan Levi <catanscout gmail.com> writes:
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;`.
Feb 08 2019
next sibling parent reply Jonathan Levi <catanscout gmail.com> writes:
Here is Sodium's GitHub page: https://github.com/SodiumFRP/sodium
Feb 08 2019
parent Alex <sascha.orlov gmail.com> writes:
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(); } } ´´´
Feb 08 2019
prev sibling next sibling parent reply Meta <jared771 gmail.com> writes:
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.
Feb 08 2019
parent reply Jonathan Levi <catanscout gmail.com> writes:
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!
Feb 08 2019
parent dayllenger <dayllenger protonmail.com> writes:
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.
Feb 09 2019
prev sibling next sibling parent reply JN <666total wp.pl> writes:
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?
Feb 08 2019
parent Jonathan Levi <catanscout gmail.com> writes:
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.
Feb 08 2019
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
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].
Feb 08 2019