Unpack a value and use part of it as a given

I have code like this:

def foo(using s: String) = ???

case class S(s: String)

val value = S("some string")
value match {
  case S(str) =>
    given s: String = str

Is there a way to declare the variable used in the unpacking (str) as given, rather than creating an explicit reference to it on the next line.

The best that I've found so far is

  case S(str) =>
    given String = str

which lets me at least avoid having a second name for the variable, but it would be nice to get it all into one line.

The real code uses a more distinctive type, so any problems related to using a very common class (String) as a given are artifacts of making a minimal example


  • You can pattern match givens with the pattern <identifier> @ given <type>, like this:

    value match {
      case S(str @ given String) => foo