Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parameter of anonymous function cannot be given ? #14167

Closed
armanbilge opened this issue Dec 23, 2021 · 3 comments
Closed

parameter of anonymous function cannot be given ? #14167

armanbilge opened this issue Dec 23, 2021 · 3 comments

Comments

@armanbilge
Copy link
Contributor

armanbilge commented Dec 23, 2021

Compiler version

3.1.1-RC1

Minimized code

Given:

class Foo:
  def bar = ()

This compiles:

Option(new Foo).map { implicit foo: Foo =>
  summon[Foo].bar
}

So does this:

for {
  given Foo <- Option(new Foo)
} yield summon[Foo].bar

But, none of these do. Am I writing it wrong, or is it just not possible?

Option(new Foo).map { given Foo =>
  // abstract givens cannot be annonymous
  summon[Foo].bar
}
Option(new Foo).map { (given Foo) =>
  // illegal start of simple expression
  summon[Foo].bar
}
Option(new Foo).map { (given foo: Foo) =>
  // illegal start of simple expression
  summon[Foo].bar
}
Option(new Foo).map { given foo: Foo =>
  // ; expected but => found
  summon[Foo].bar
}
@som-snytt
Copy link
Contributor

Option(C()).map { ((x: C) ?=> summon[C])(using _) }

@som-snytt
Copy link
Contributor

Option(C()).map { case given C => summon[C] }

As mentioned under "pattern-bound givens", same as for given.

https://docs.scala-lang.org/scala3/reference/contextual/givens.html

@armanbilge
Copy link
Contributor Author

Aha! That's a reasonable syntax, thanks :)

I'd still love to see something similar to Scala 2, if possible, so we can skip the case keyword.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants