In fact, you can model the behavior of groups of methods with partial functions. However, this is probably not recommended because you are causing some type of error at run time, and also incur some costs to determine which congestion needs to be caused. However, does this code do what you want?
object MethodGroup extends App { //The return type of "String" was chosen here for illustration //purposes only. Could be any type. val DoSomething: Any => String = { case () => "Do something was called with no args" case (x: Int) => "Do something was called with " + x case (x: Int, y: Int) => "Do something was called with " + (x, y) } //Prints "Do something was called with no args" println(DoSomething()) //Prints "Do something was called with 10" println(DoSomething(10)) //Prints "Do something was called with (10, -7)" println(DoSomething(10,-7)) val x = Set((), 13, (20, 9232)) //Prints the following... (may be in a different order for you) //Do something was called with no args //Do something was called with 13 //Do something was called with (20, 9232) x.map(DoSomething).foreach(println) }
source share