Can std.variant be used with std.container.rbtree?
Vijay Nayar
madric at gmail.com
Sat Apr 2 14:35:10 UTC 2022
On Saturday, 2 April 2022 at 10:04:49 UTC, vit wrote:
> Try use ```std.sumtype```.
I'm playing with SumType to see how it works, and I must be doing
something silly, because it fails to compile with the first
example type I attempted. Consider the following:
```d
import std.sumtype;
import std.algorithm : cmp;
alias VarType = SumType!(double, ubyte[]);
int opCmp(const ref VarType v1, const ref VarType v2) {
alias doMatch = tryMatch!(
(ref ubyte[] _1, ref ubyte[] _2) => cmp(_1, _2),
(_1, _2) => _1 < _2 ? -1 : (_2 < _1 ? 1 : 0));
return doMatch(v1, v2);
}
void main() {
VarType b1 = cast(ubyte[]) [0x01, 0x02, 0x03];
VarType b2 = cast(ubyte[]) [0x01, 0x02, 0x04];
b1 > b2;
}
```
The `tryMatch` method fails to compile, and instead I get the
following error:
```d
/dlang/dmd/linux/bin64/../../src/phobos/std/sumtype.d(2004):
Error: static assert: "`handlers[0]` of type `int function(ref
ubyte[] _1, ref ubyte[] _2) pure nothrow @nogc @safe` never
matches"
/dlang/dmd/linux/bin64/../../src/phobos/std/sumtype.d(1739):
instantiated from here: `matchImpl!(const(SumType!(double,
ubyte[])), const(SumType!(double, ubyte[])))`
onlineapp.d(10): instantiated from here:
`tryMatch!(const(SumType!(double, ubyte[])),
const(SumType!(double, ubyte[])))`
```
I'm not super sure why this handler would not match. If I do
simple types like `double` or `int` it works, it even works with
`string`, but the moment I include `ubyte[]`, it no longer
compiles. In this example, I eliminated all other array types
aside from `ubyte[]`, so there should be no conflicts in theory.
More information about the Digitalmars-d-learn
mailing list