4

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?

1 Answer 1

5

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")
Sign up to request clarification or add additional context in comments.

1 Comment

@PaulDraper - So you want Foo.foo to be generic?

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.