How to type alias with BitOr?

Just for brevity in my match code I'd like to alias some primitive types. How do I do this for a type implementing BitOr?

// polars_core::datatypes::dtype
use DataType::{Float64, Int32};

pub const PRIM: dyn DataType + BitOr = Int32 | Float64;

...am I close?

It's not clear what you're asking for. polars_core::datatypes::DataType is an enum that does not implement BitOr. It's not a trait, so dyn DataType doesn't make sense. Can you restate what your goal is? Can you give a code sample without attempts at brevity that compiles and demonstrates how you intend to use this alias?

Oh, are you asking for an alias you can use in a match, like this?

match data_type {
    DataType::Int32 | DataType::Float64 => { ... }
    // replaced by...
    PRIM => { ... }

The only way to do that is to define a macro. The | here is not a BitOr operator but part of pattern syntax — it's not any single value.

1 Like

Thanks - you got the right answer at the tail end of your solution.

I suppose it is generally true that macros can help reduce large match branches by stuffing them inside a macro_rules! definition.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.