correct Scala 3 syntax for providing a given from a higher order function argument

Viewed 45

In Scala 2 I could have written something like this:

// a function that needs an implicit context
def funcWithCtx(arg1: String)(implicit ctx: Context) = ???

myObj.doSomething { implicit ctx => // mark argument as the implicit context
  funcWithCtx("arg1")
}

This syntax works in Scala 3, but I thought implicit was being deprecated and given \ using was being used instead? I've tried to replace implicit with given but the compiler doesn't like that.

myObj.doSomething { given x => // this fails!
  ...
}

Is this one place where the implicit keyword is still required?

1 Answers

I guess, correct is

myObj.doSomething { ctx =>
  given Context = ctx
  ...
}
Related