1

Below code works fine

  def exec(f: (Int, Int) => Boolean, p1: Int, p2: Int) = f(p1, p2)
  val >= = (x1: Int, x2: Int) => x1 >= x2
  println(exec(>=, 10, 10))

however the question is, if it's possible to get it working without explicit re-defining operator (synthetic function)?

Update

It's clear that it works absolutely fine like this

println(exec(_ >= _, 10, 10))

The question is whether it's possible to make it working in a form exec(>=, 10, 10) without defining functional value.

Dr Y Wit
  • 2,000
  • 9
  • 16

2 Answers2

2

Complementing my other answer you may do this:

println(exec(10, 10)(_ >= _))
  • As shown in your other answer currying helps to infer parameters' type in Scala 2 (Scala 3 can do that without multiple parameter lists). I do not think currying makes any difference here. Question is more about whether there is any way to omit placeholders (underscores). – Dr Y Wit Jun 01 '20 at 19:05
  • @DrYWit yeah AFAIK, there is no way to transform the symbolic method to a function without an alias or the place holder. – Luis Miguel Mejía Suárez Jun 01 '20 at 19:10
0

Let me try to answer my own question.

Comparison methods are declared in Int in a following way

def>=(x: Int): Boolean

So they are called for an instance of Int and applied to one argument. There is no way to pass such methods as parameters without "binding" them with two arguments.

We can do this by declaring method or functional value in advance and map it to >= method in Int like below

def >= (x1: Int, x2: Int) = x1 >= x2
def >= = (_: Int) >= (_: Int)
val >= = (x1: Int, x2: Int) => x1 >= x2
val >= = (_: Int) >= (_: Int)

or by using anonymous function in a form _ >= _.

Dr Y Wit
  • 2,000
  • 9
  • 16