digitalmars.D - Nothing type/value aka Unit
- Jonathan Levi (13/13) Feb 08 2019 In D what would be the most appropriate type for a value that
- Jonathan Levi (1/1) Feb 08 2019 Here is Sodium's GitHub page: https://github.com/SodiumFRP/sodium
- Alex (32/34) Feb 08 2019 So... you want to return something, and void is not possible?
- Meta (7/20) Feb 08 2019 There's no canonical unit type in D, so I'd recommend going with
- Jonathan Levi (6/7) Feb 08 2019 Did not know that existed.
- dayllenger (5/8) Feb 09 2019 void[0] is better, because it almost always has exactly 0 bytes
- JN (2/15) Feb 08 2019 how about Nullable!Event?
- Jonathan Levi (4/5) Feb 08 2019 What is needed is more like Event!Unit.
- Timon Gehr (2/4) Feb 08 2019 I usually use void[0].
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
Here is Sodium's GitHub page: https://github.com/SodiumFRP/sodium
Feb 08 2019
On Friday, 8 February 2019 at 13:33:23 UTC, Jonathan Levi wrote:Here is Sodium's GitHub page: https://github.com/SodiumFRP/sodiumSo... 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
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
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
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
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
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
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