New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question about mir.algebraic.visit #34
Comments
Thanks for the report, I will take a look. As for now, small simplification |
Yes, this is by design. I am a bit conservative in case of adding special overloading resolution similar to other libs, this needs a lot of things to think about like classes/interface inheritance, Instead, it seems to better add another one set of handles that allow splitting algebraic type into sub-types like: alias IntegralValue = Variant!(byte, ubyte, short, ushort, int, uint, long, long);
...
v.splitVisit!(
(string v) { writeln("str: ", v); },
(const(void)[] v) { writeln("raw: ", v); },
(Bool v) { writeln("bit: ", cast(bool)v ? "true" : "false"); },
(float v) { writefln("f32: %.8f", v); },
(double v) { writefln("f64: %.15f", v); },
(IntegralValue v) { v.visit!( v => writeln("int: ", v)); }, // int's
); Two workarounds for now: /+ dub.sdl:
name "app"
dependency "mir-core" version="1.1.54"
+/
version = short_visit;
import mir.algebraic;
import std.stdio;
enum Bool : bool { false_ = false, true_ = true }
alias Value = TaggedVariant!(
["str", "raw", "bit", "i8", "u8", "i16", "u16", "i32", "u32", "i64", "u64", "f32", "f64"],
string, const(void)[], Bool, byte, ubyte, short, ushort, int, uint, long, ulong, float, double,
);
version = visit_with_static;
void foo(in Value val)
{
import std.traits;
version (visit_with_static)
{
static void f32Handle(T)(const T v) if (is(T == float)) { writefln("f32: %.8f", v); };
static void f64Handle(T)(const T v) if (is(T == double)) { writefln("f64: %.15f", v); };
static void intHandle(T)(const T v) if (isIntegral!T) { writeln("int: ", v); }; // int's
val.visit!(
(string v) { writeln("str: ", v); },
(const(void)[] v) { writeln("raw: ", v); },
(Bool v) { writeln("bit: ", cast(bool)v ? "true" : "false"); },
f32Handle,
f64Handle,
intHandle,
);
}
else
{
val.visit!(
(string v) { writeln("str: ", v); },
(const(void)[] v) { writeln("raw: ", v); },
(Bool v) { writeln("bit: ", cast(bool)v ? "true" : "false"); },
(v) {
alias T = Unqual!(typeof(v));
static if (is(T == float))
writefln("f32: %.8f", v);
else
static if (is(T == double))
writefln("f64: %.8f", v);
else
writeln("int: ", v);
}
);
}
}
unittest { foo(Value(10)); }
|
@deviator Kind based get/trustedGet has been added. |
Hello! I have this code:
With
version = short_visit;
orversion = full_visit;
all works.With
version = visit;
it get compile time error:but it have last "default" function without type specification for argument what can be instanced for all int types.
This is a bug or it by design?
Original code is more complex and I found useful variant with
switch
by now (the desire to haveget
by kind from here).The text was updated successfully, but these errors were encountered: