How can I define an anonymous generic Scala function?
Asked Answered
M

3

20

Let's say I have this:

val myAnon:(Option[String],String)=>String = (a:Option[String],defVal:String) => {
  a.getOrElse(defVal)
}

Don't mind what the function does. Is there anyway of making it generic, so I can have an Option[T]?

Maneuver answered 31/3, 2010 at 16:9 Comment(0)
A
10

To summarize from that answer: No, you can't make anonymous functions generic, but you can explicitly define your function as a class that extends one of the Function0, Function1, Function2, etc.. traits and define the apply function from those traits. Then the class you define can be generic. Here is the excerpt from the original article, available here:

scala> class myfunc[T] extends Function1[T,String] {
     |     def apply(x:T) = x.toString.substring(0,4)
     | }
defined class myfunc

scala> val f5 = new myfunc[String]
f5: myfunc[String] = <function>

scala> f5("abcdefg")
res13: java.lang.String = abcd

scala> val f6 = new myfunc[Int]
f6: myfunc[Int] = <function>

scala> f6(1234567)
res14: java.lang.String = 1234
Anaximenes answered 2/4, 2010 at 14:21 Comment(1)
Can also write it more idiomatically as extends (T => String) {Heshvan
H
7

I don't think anonymous functions can have type parameters. See this answer for details.

Holbrook answered 31/3, 2010 at 16:36 Comment(0)
T
0

14 years later... on Scala 3.4

val GenericFunction = [A] => (Option[A], A) => A
// Type can be inferred.
val genericFunction : GenericFunction =
  // ...but not _in_ the actual lambda.
  // I don't understand how the type inference works, but, it doesn't:
  //   You're gonna have to annotate the function on declaration,
  //   just a tad ~~annoying~~.
  [A] => (a: Option[A], defaultValue: A) =>
    a.getOrElse(defaultValue)

// Wrapping it to demonstrate call.
def genericMethod[A](a: Option[A], defaultValue: A): A =
  // Type parameter inferred.
  genericFunction(a, defaultValue)

Tip #1: Difference between:
  1. Generic (polymorphic) function type
    • type GenericLambda = [A] => (A, A) => A
  2. Parameterized type
    • type ParameterizedType[A] = (A, A) => A
  3. Type lambda:
    • type TypeLambda = [A] =>> (A, A) => A

1 is not like the others. And the one we care about for this question.

1 & 3 are easy to confuse for each other.

2 & 3 are interchangeable as far as I understand, but I try to use parameterized types. When I need the big guns then I use type lambda, as a sort of mnemonic.

If you're learning about generic functions—and without getting into the experimental—here are more functional (as in, related to functions) concepts/notation that might be useful: Contextual functions (?=>)Dependent functionsMatch types


Tip #2: Can't extend generic function: Non-abuse case.
class Example1(val function: (Int, Int) => Unit) extends ((Int, Int) => Unit):
  def defApply(a1: Int, a2: Int): Unit = function(a1, a2)
  val valApply : function.type = function

// Invalid, write as [[Example3]] instead.
class Example2(val function: [A, R] => (A, A) => R) extends ([A, R] => (A, A) => R):
  // -----------------------------------------------------> ~~~~~~~~~~~~~~~~~~~~~~~
  // | [A, R] => (x$1: A, x$2: A) => R is not a class type
  def defApply[A, R](a1: A, a2: A): R = function(a1, a2)
  val valApply : function.type = function

class Example3[A, R](val function: (A, A) => R) extends ((A, A) => R):
  def defApply(a1: A, a2: A): R = function(a1, a2)
  val valApply : function.type = function

Example1((a, b) => println(s"a: $a, b: $b"))
Example3[Int, Unit]((a, b) => println(s"a: $a, b: $b"))
Therein answered 26/3 at 21:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.