I’m thinking of how to write an abstract base class with an abstract method that will take either one ro two Int values. Something like:
abstract class Foo {
def doSomething(???): Unit
}
The best I can think of so far is declaring a parameter as a Vector[Int] that will either hold one or two values, or perhaps to better enforce the maximum of two values a Tuple2[Int, Int].
I’d like to do this as I want to pass around algorithms within an Actor system, and I want certain messages to enforce the type of the algorithm instead of passing around an Any, hence the abstract base class.
Is this the best way, or are there better ways?
Typically this would just be done with an overloaded method. Then when calling the method on
Bar, they can pass in either one or two things:Or alternatively, using a default parameter (which still allows you to do
new Bar().doSomething(5))But it sounds like you want to do this in a way where you always call the same
Foomethod, so in that case, you can do the polymorphism on the parameter:Or, since you just have two choices, you can do the same thing with an
Either: