Is it possible to pattern match functions in scala ? Specially the return type of the function.
Here is an example. I'm trying to print Shoe if the function's return type is Shoe or Bag otherwise.
object Test extends App {
trait ProductItem {
val name: String
val price: Int
}
case class Product(partialPerson: (String) => ProductItem)
case class Shoe(name: String)(val price: Int) extends ProductItem
case class Bag(name: String)(val price: Int) extends ProductItem
val shoe = Shoe("nike")(_)
val bag = Bag("addidas")(_)
def printType(shoe: (Int) => ProductItem): Unit = {
shoe match {
case isShoe: ((Int) => Shoe) =>
println("Is a shoe")
case isBag: ((Int) => Bag) =>
println("Is a bag")
}
}
printType(shoe)
printType(bag)
}
Output:
Is a shoe
Is a shoe
Expected output:
Is a shoe
Is a bag