How can I save an object of unknown type in Scala and return it?
I have a couple of classes that are all instantiated from the same class like this
class And(lhs: ExprTree, rhs: ExprTree) extends ExprTree
class Or(lhs: ExprTree, rhs: ExprTree) extends ExprTree
and a function that needs to return an instance of said classes (ie And(lhs, rhs)
but I have no prior knowledge which). I also need to store an instance of And()
or Or()
without prior knowledge which one it is.
val expr: ExprTree = ???
will give you an identifier which could take on the value of either an And
or an Or
.
Similarly,
def (...): ExprTree = ???
will give you a function which will return a value of either And
or Or
(assuming some appropriate parameters, represented here by ...).
This answer seems far too simple and obvious. But it also seems to satisfy what I think is your request.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.