I have this enum:
pub enum Op {
Add,
Sub,
Mul,
Div,
}
pub enum Primitive {
Number(f64),
String(String),
Iterable(IterableKind),
Graph(Graph),
GraphEdge(GraphEdge),
GraphNode(GraphNode),
Tuple(Tuple),
Boolean(bool),
Undefined,
}
and i want to do things like "binary operators" between any of 2 primitives (for example i want to be able to define an operator for String + number etc)
I tried to do this:
trait ApplyOp{
type Target;
fn apply_op(&self, op: Op, to: &Self::Target) -> Result<Self::Target, OperatorError>;
}
trait ApplyToPrimitiveOp: ApplyOp<Target = Primitive> {
}
impl ApplyToPrimitiveOp for i32 {
pub fn apply_op(&self, op: Op, to: Primitive) -> Result<Primitive, OperatorError>{
todo!()
}
}
//i'd have to do the same for String, IterableKind, Graph, GraphEdge etc...
but i get the error:
`fn apply_op` is not a member of trait `ApplyToPrimitiveOp`rust-analyzerE0407
method `apply_op` is not a member of trait `ApplyToPrimitiveOp`
not a member of trait `ApplyToPrimitiveOp`
My end goal is to be able to do:
Exp::BinaryOperation(op, lhs, rhs) => {
//TODO implement operator overloading for every primitive
let lhs = lhs.as_primitive(context)?;
let rhs = rhs.as_primitive(context)?;
lhs.apply_op(op, rhs)
}
How could i do it?