www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - Bitblob (hash wrapper) & Minivariant (tagged union) dub packages

reply Mathias Lang <pro.mathias.lang gmail.com> writes:
Hi everyone!
I usually don't post on the forums, but I got two small projects 
I developed as helpers for some other projects, and figured some 
people might have interest in them. They are registered on dub.



https://github.com/Geod24/bitblob

Bitblob is a simple wrapper for hashes. While it was almost 
exclusively tested with 256 bits hash, it should work with any 
hash size.
It's a value type with 0 overhead (as in (Bitblob!256).sizeof == 
32), can be initialized from an hex string or the result of 
`std.digest.sha : shaXXXOf()` (which is an `ubyte[XXX/8]`).
It also provides allocation-less string representation via the 
`void toString(scope void delegate(const(char)[]))` method.




https://github.com/Geod24/minivariant

Minivariant is a "works for me" replacement of `std.variant : 
Algebraic`.
I just wanted a simple tagged union that did the job, but the 
following code does not compile:
```d
import std.variant : Algebraic;

void main ()
{
     immutable int a = 42;
     Algebraic!(int, bool, string) var;
     var = a;
}
```

```
dlang/dmd/std/variant.d(628): Error: static assert:  "Cannot 
store a immutable(int) in a VariantN!(16LU, int, bool, string). 
Valid types are (int, bool, string)"
wat.d(7):        instantiated from here: opAssign!(immutable(int))
```

I looked into fixing it, but given Variant's design (which relies 
heavily on `TypeInfo`) it would have been a wild goose chase. And 
I know I'm not the only one that needed his own tagged union (see 
e.g. https://github.com/s-ludwig/taggedalgebraic). It also has a 
`visit` method similar to std.variant, although not with 
delegates, as there is currently no way to merge delegates into 
an overload set, so you have to use an existing overload set (see 
the examples).


The README of each repo contains more details and examples, and 
the code is usually documented.
Jan 22 2019
parent Paul Backus <snarwin gmail.com> writes:
On Tuesday, 22 January 2019 at 10:26:33 UTC, Mathias Lang wrote:


 https://github.com/Geod24/minivariant

 Minivariant is a "works for me" replacement of `std.variant : 
 Algebraic`.

 [...]

 I looked into fixing it, but given Variant's design (which 
 relies heavily on `TypeInfo`) it would have been a wild goose 
 chase. And I know I'm not the only one that needed his own 
 tagged union (see e.g. 
 https://github.com/s-ludwig/taggedalgebraic). It also has a 
 `visit` method similar to std.variant, although not with 
 delegates, as there is currently no way to merge delegates into 
 an overload set, so you have to use an existing overload set 
 (see the examples).
I've also been through this exact process. The resulting package is on dub as `sumtype` [1]. In addition to solving the problem you noticed with `immutable`, it also works with safe, nogc, pure, and nothrow, and supports both delegates and overload sets with its `match` method (the equivalent of `visit`). [1] http://sumtype.dub.pm/
Jan 22 2019