For extracting the underlying value from the enum variant I have two options either use match or an if let. For some specific cases, I am sure what the enum variant is going to be, and I am doing something like this to extract value
let left: Object; // <-- got from function return
// For IntExpr
let val: Option<i64> = if let Object::Number { token: _, value } = left {
Some(value)
} else {
None
};
Then I use values with .unwrap() as it is always going to ok with no chance of panics.
In Golang, I can do something like this (with interfaces)
val := left.(*object.Number).Value
Is there any optimal or correct way to do what I am doing to extract values of enums?
Why do you think that the current approach is incorrect or suboptimal? Have you found a case where it works erroneously? Have you benchmarked and found that it is too slow?