0

I have several functions with same input and output types initialized in an object

object Utils {
  def f1(value: Int): Double = ???
  def f2(value: Int): Double = ???
  def f3(value: Int): Double = ???
}

I have a list of higher order values for those functions:

val x = List(Utils.f1, Utils.f2)

How can I use pattern matching to check which functions of those declared in the object are contained in x? I would like to obtain something similar to the following code:

x(0) match {
  case Utils.f1 => ...
  case Utils.f2 => ...
}
3
  • 3
    This is not possible in the general sense, function equality is a NP-Hard problem, but they may be subtle workarounds. However, needing to know which function you have feels weird, this seems like an XY-Problem, care to explain why exactly do you want to do this, what is the meta-problem behind the question? Commented Oct 27, 2020 at 15:23
  • 1
    In my project I have a set of objects where one of their value is a higher order function, this value describes the behaviour of the object to some events happening. Based on this value, I want to represent these objects differently in the GUI. Commented Oct 27, 2020 at 15:33
  • 3
    You can create your own ADT to encapsulate those functions, so you can pattern match against that to determine which function you have. Commented Oct 27, 2020 at 15:39

2 Answers 2

4

This will be possible if you make f1, f2, f3 vals. Please notice that upon pattern matching the equality by reference will be used

object Utils {
  val f1: Int => Double = _ * 10.0
  val f2: Int => Double = _ * 20.0
  val f3: Int => Double = _ * 30.0
}

val x: Seq[Int => Double] = List(Utils.f1, Utils.f2)

import Utils._

x(0) match {
  case `f1` => println(1)
  case `f2` => println(2)
  case `f3` => println(3)
}

If you keep f1, f2, f3 defs then

object Utils {
  def f1(value: Int): Double = value * 10.0
  def f2(value: Int): Double = value * 20.0
  def f3(value: Int): Double = value * 30.0
}

val x: Seq[Int => Double] = List(Utils.f1, Utils.f2)
  
val f1: Int => Double = Utils.f1
val f2: Int => Double = Utils.f2
val f3: Int => Double = Utils.f3

x(0) match {
  case `f1` => println(1)
  case `f2` => println(2)
  case `f3` => println(3)
}

produces MatchError.

Sign up to request clarification or add additional context in comments.

4 Comments

What if the input type is generic? def standardFoodEffect[A <: Int](value: A): Set[Double] How can i convert this into val?
@Snorlite In Scala 2 there are no polymorphic functions as first-class citizens. You'll have to emulate them with wrappers trait Poly { type B; def apply[A](a: A): B } val standardFoodEffect = new Poly { type B = Set[Double]; def apply[A <: Int](a: A) = ??? } like shapeless.Poly.
Or trait Poly[Upper] {type B; def apply[A <: Upper](a: A): B}, val standardFoodEffect = new Poly[Int]...
4

You can't match against functions, it has no meaningfully defined equality.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.