Sane
Sane

Reputation: 2554

By-name type parameters

Imagine I have the following class definition:

class Foo[T]

and I want to do the following

def bar(x:Foo[ =>Int ]):Int = ???

But compiler fails with "no by-name parameter type allowed here"

How can I use a by-name type as type parameter for a generic method?

Upvotes: 6

Views: 1055

Answers (1)

Jean-Philippe Pellet
Jean-Philippe Pellet

Reputation: 59994

You’ll have to provide your own lazy wrapper. Something like this:

class Lazy[T](wrp: => T) {
  lazy val value: T = wrp
}

and then:

def bar(x: Foo[Lazy[T]]): Int = ???

Upvotes: 10

Related Questions