I have two objects, ObjectA and ObjectB, both with a method update(). I want to write a function that accepts either ObjectA or ObjectB (but no other types). Conceptually, this is what I am trying to do:
def doSomething[T <: ObjectA | T <: ObjectB](obj: T) = {
obj.update
}
I realize there are other ways to solve this problem (eg, structural typing of the update() method, common base class, etc) but my question is it is possible to do it this way in Scala and if so what is the syntax? And what is this called?
T
had if it didn't know what type it was? If you mean to make sure your class has anupdate
, then that's what structural types are for. – Crack