Compile time check if two Types are Equal

Note, all this is happening compile time. True, False being types is intended. It is not a mistake.

Suppose we have:

pub struct True {};
pub struct False {};

is it possible to design a "IsEqual" so that "A IsEqual A => True", "A IsEqual B => False" for A != B

One possible approach is:

pub trait IsEqualT<Target> {
  type BoolOutput;

(A as IsEqualT<B>)::BoolOutput

This technique can be made to work, but all the structs, 'A', and 'B' must be declared via a procedural macro which injects a Uid field containing a typenum number: lhlist/ at master · agnes-rs/lhlist · GitHub


Question: is there a simpler way to do this?

Probably requires specialization.

Yeah, with specialization this wouldn't be terribly difficult to accomplish. Without it I don't believe it's possible.

1 Like