Oli
Oli

Reputation: 1142

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

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?

Upvotes: 1

Views: 129

Answers (1)

Dmytro Mitin
Dmytro Mitin

Reputation: 51648

I guess, correct is

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

or

myObj.doSomething { case ctx@given Context =>
  ...
}

Allow given bindings in patterns

Upvotes: 1

Related Questions