5

I want to define a function with implicit parameter in a way like this:

// imports to add scope of A

{
  implicit a: A => {
    // some action
  }
}.apply()

// somewhere in the code           

class A

val a: A = new A

But my Scala compiler doesn't compile it. It says: Cannot resolve reference apply with such signature. However, the parameter is implicit, so I guess compiler should look up in the scope and find an appropriate object.

Is it true? If not, then how can I fix it?

3
  • It's really not clear what you want to achieve, at least to me Commented Apr 23, 2016 at 10:55
  • I want to fire a function and force it to use implicit param. I don't want to pass the param to the function explicitly. Commented Apr 23, 2016 at 10:59
  • Oh ok I get what you mean, I skipped the 'anonymous' part of the question. You can't then, only methods can have implicit parameters Commented Apr 23, 2016 at 11:10

2 Answers 2

8

You can't. Only methods can have implicit parameters.

When you do this:

// val f: A => Unit = 
{
   implicit a: A => {
     // some action
   }
}

you're actually declaring an anonymous function of type A => Unit and you are declaring the argument a as implicit in the function body


You can achieve something close to what you want using the magnet pattern:

class A

case class Magnet()
object Magnet {
  implicit def fromUnit(arg1: Unit)(implicit a: A) = Magnet()
}

object Test extends App {

  implicit val a = new A

  {
    args: Magnet => {
      //...
    }
  }.apply()
}

You'll get a deprecation warning though because the magnet must have at least one parameter and I used Unit, you should call it like .apply(()) to avoid it

Sign up to request clarification or add additional context in comments.

Comments

3

As said by Giovanni: You can't have such a parameter.

However you can use implicitly to resolve implicits within your function:

case class Foo(text : String)
implicit val foo = Foo("World")

(() => {
  val implFoo : Foo = implicitly[Foo]
  println(s"Hello ${implFoo.text}")
}).apply()

(But to be honest this sounds like it can be written better and you're going into spaghetti code territory with what you're doing.)

Comments

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.