5

Assume we have Object that takes function as parameter of apply method:

object Wrapper {
  def apply(block: TypeA => String) = {
    TypeA a = ...
    block(a)
  }
}

TypeA is domain type of application.

Now when I define inline block I can define TypeA parameter as implicit:

Wrapper { implicit a => functionThatUseImplicitA() } 

But what if block parameter is not Function1, but Function2? How can I define both parameters as implicit?

object Wrapper2 {
  def apply(block: (TypeA, TypeB) => String) = {
    TypeA a = ...
    TypeB b = ...
    block(a, b)
  }
}

This one does not work:

Wrapper { implicit (a, b) => functionThatUseImplicitAB() } 

The only workaround is define them as vals:

Wrapper { (a, b) => 
  implicit val ia = a
  implicit val ib = b
  functionThatUseImplicitAB()
} 

Thanks!

1esha
  • 1,722
  • 11
  • 17

1 Answers1

8

According to the SLS 6.23 Anonymous Functions implicit keyword is allowed only for single argument functions:

Expr ::= (Bindings | [‘ implicit ’] id | ‘_’) ‘=>’ Expr
ResultExpr ::= (Bindings | ([‘ implicit ’] id | ‘_’) ‘:’ CompoundType) ‘=>’ Block

So you can't make two function parameters as implicit.

This is the one of reasons to use curried functions:

object Wrapper {
  def apply(block: TypeA => TypeB => String) = ???
}

Wrapper { implicit a => implicit b =>
  functionThatUseImplicitAB()
} 
senia
  • 37,745
  • 4
  • 88
  • 129
  • Could you please tell me how to solve this issue with curried functions? functionThatUseImplicit* are from external API – 1esha Jun 25 '13 at 11:57
  • @AlexeyRomantchouk: I've updated my answer. It solves your problem when you can change definition of `Wrapper`. – senia Jun 25 '13 at 12:00