Bitblob (hash wrapper) & Minivariant (tagged union) dub packages

Mathias Lang pro.mathias.lang at
Tue Jan 22 10:26:33 UTC 2019

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.

## 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.

## Minivariant

Minivariant is a "works for me" replacement of `std.variant : 
I just wanted a simple tagged union that did the job, but the 
following code does not compile:
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. 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.

More information about the Digitalmars-d-announce mailing list