Enums, Any, and PartialEq

Is it possible to qualify the Any type to say that the things you’re holding implement specific traits (e.g., PartialEq)? Here’s my case:

#[derive(Clone,Debug,PartialEq)]
enum Datum {
    Int(i64),
    Flt(f64),
    List(Rc<MyList>),
    Other(Rc<Any>)
}

See below for the motivation for this. Point is, I want to Datum to be able to contain values of whatever type is needed I want to put in, and get it out again; but I also want to constrain the other type to be PartialEq (and maybe other traits as well).

Any ideas? I’m not married to using Any, or enums, or whatever, but the above snippet shows well enough what I want.

Background: This is for a language interpreter whose variables may be of various types. The language is to be implemented as a library crate, to be used by Rust apps; and the intent is that the user of the crate should be able to extend the language’s type system. Consequently, the answer isn’t to simply extend the enum with the specific type. (The language in question is TCL; and in the standard C implementation of TCL this is a common way of introducing domain-specific types into the language.)

After doing some more thinking, I find that I don’t really need PartialEq in this case; but the base question remains: given the above enum, is there a way to tell the Rust compiler that the values I’m sticking in the Other(Rc<Any>) slot are constrained to have particular traits?

Not directly, you can do this though

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=ada742b81ce081762e8754a76a8588a0

1 Like

Excellent, thank you! Still pretty much a newbie, me.

Cool, if you need more help feel free to message me or post again!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.