Hacker News new | ask | show | jobs
by ojeda 1761 days ago
C++ may get pattern matching with P1371, which may make visiting look more reasonable:

    std::string_view print_type(const variant_type& jtt) {
        using namespace std::literals;
        return inpect (jtt) {
            <double> __ => "Number"sv;
            <bool> __ => "Bool"sv;
            ...
        };
    }
In any case, `std::variant` should have been a language feature all along, instead of a library one; cf. Rust:

    enum Value {
        Number(f64),
        Bool(bool),
        ...
    }

    fn print_type(v: &Value) -> &str {
        match v {
            Value::Number(_) => "Number",
            Value::Bool(_) => "Bool",
            ...
        }
    }
1 comments

Yes, pattern-matching like this would be the bee's knees