Re:Easy Mode Rust
by Galanwe on Friday April 05, 2024 @09:52AM
(#39942390)
I agree, the lack of pattern matching is a bummer. You should retry std::variant with the lambda overload trick though. It's kind of okay in terms of syntax.
[ Parent
| on HN ]
Re:Easy Mode Rust
by coffeeaddict1 on Friday April 05, 2024 @10:12AM
(#39942598)
You can even do better with something like:
namespace StlHelpers {
template<class... Ts> struct overload : Ts... { using Ts::operator()...; };
template<class... Ts> overload(Ts...) -> overload<Ts...>;
template<class var_t, class... Func> auto visit(var_t & variant, Func &&... funcs)
{
return std::visit(overload{ funcs... }, variant);
}}
And then
StlHelpers::visit(var,
[](MyType1 t1) {...},
[](MyType2 t2) {...},
[](auto t) {...}
);