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

@static annotation does not work for given #19304

Closed
armanbilge opened this issue Dec 20, 2023 · 5 comments · Fixed by #19318
Closed

@static annotation does not work for given #19304

armanbilge opened this issue Dec 20, 2023 · 5 comments · Fixed by #19318
Assignees
Labels
Milestone

Comments

@armanbilge
Copy link
Contributor

armanbilge commented Dec 20, 2023

Compiler version

3.3.1

Minimized code

import scala.annotation.static
trait Foo[A]
object Foo:
  @static given foo: Foo[String] = new {}

Output

4 |  @static given foo: Foo[String] = new {}
  |  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |  Lazy @static fields are not supported
1 error found

Expectation

Declaring it as an implicit val compiles fine. So my expectation is that there is a way to do this with given as well.

@static implicit val foo: Foo[String] = new {}

An implicit def also works.

@static implicit def foo: Foo[String] = new {}
@armanbilge armanbilge added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 20, 2023
@nicolasstucki nicolasstucki added itype:enhancement area:implicits related to implicits and removed itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 20, 2023
@odersky
Copy link
Contributor

odersky commented Dec 20, 2023

This should work:

@static private val myFoo: Foo[String] = new {}
@static given Foo[String] = myFoo

@odersky odersky closed this as completed Dec 20, 2023
@nicolasstucki
Copy link
Contributor

Unfortunately it does not work:

import scala.annotation.static
trait Foo[A]
object Foo:
  @static private val myFoo: Foo[String] = new {}
  @static given Foo[String] = myFoo // error: Lazy @static fields are not supported

@nicolasstucki
Copy link
Contributor

The issue is that given Foo[String] = myFoo becomes a lazy val and not a def.

We could force it to be a def with @static given (using DummyImplicit): Foo[String] = myFoo, but this will not work in all situations.

@odersky
Copy link
Contributor

odersky commented Dec 20, 2023

Would simply dropping the @static on the given work?

Never mind, I think we can fix this by tweaking the way givens are translated.

@odersky
Copy link
Contributor

odersky commented Dec 20, 2023

The should work now:

import scala.annotation.static
trait Foo[A]
object Foo:
  @static val myFoo: Foo[String] = new {}
  @static given Foo[String] = myFoo

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

Successfully merging a pull request may close this issue.

4 participants