Paul Draper
Paul Draper

Reputation: 83383

Scala generic functions

I have an abstract class

abstract class Foo {
  def foo(a: Int): Int
  ...
}

// Usage
new Foo {
  def foo(a: Int) = {
    println("Foo")
    a
  }
}

I frequently see a companion object to make this a little less verbose for callers (e.g. the Play framework).

object Foo {
  def apply(f: Int => Int) = new Foo {
    def foo(a: Int) = f(a)
  }
}

// Usage
Foo { a =>
  println("Foo")
  a
}

But suppose I make the method generic

abstract class Foo {
  def foo(a: T): T
  ...
}

// Usage
new Foo {
  def foo(a: T) = {
    println("Foo")
    a
  }
}

Can I still use a companion object, i.e. can I apply generic type parameters to a function, rather than a method or class?

Upvotes: 4

Views: 1591

Answers (1)

Lee
Lee

Reputation: 144206

Yes you can do this by emulating rank 2 polymorphism. Based on this article you can do:

trait ~>[F[_],G[_]] {
  def apply[A](a: F[A]): G[A]
}

type Id[A] = A

abstract class Foo {
  def foo[T](a: T): T
}

object Foo {
  def apply(f: Id ~> Id) = new Foo {
    def foo[T](a: T): T = f(a)
  }
}

val fun = new (Id ~> Id) { def apply[T](a: T): T = { println("Foo"); a } }
val foo = Foo(fun)
foo.foo(1)
foo.foo("String")

Upvotes: 5

Related Questions