Skip to content

constValueOpt doesn't work for class parameters #17387

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

Closed
doofin opened this issue May 1, 2023 · 11 comments
Closed

constValueOpt doesn't work for class parameters #17387

doofin opened this issue May 1, 2023 · 11 comments
Labels
itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label

Comments

@doofin
Copy link

doofin commented May 1, 2023

Compiler version

3.2.2

Minimized code

import scala.compiletime.*

class c2[i <: Int] { println("constValueOpt:" + constValueOpt[i]) }
new c2[2]

Output

constValueOpt:None

Expectation

should be constValueOpt:Some(2)

@doofin doofin added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels May 1, 2023
@soronpo
Copy link
Contributor

soronpo commented May 2, 2023

This is not a bug. When the class is being compiled the compiler has no knowledge what i type is, and that is when the class body, along with constValueOpt is being compiled as well.

@soronpo soronpo closed this as completed May 2, 2023
@soronpo
Copy link
Contributor

soronpo commented May 2, 2023

Here is a better way:

import scala.compiletime.*

class c2[i <: Int]:
  inline def printme: Unit = println("constValueOpt:" + constValueOpt[i])
new c2[2].printme

@doofin
Copy link
Author

doofin commented May 2, 2023

thanks for the reply, I know inline def will work,but feel that this limitation is not necessary. could you point me to the LOC in the compiler ?

@doofin
Copy link
Author

doofin commented May 2, 2023

so according to the code, constValueOpt is only supported at inlinedMethod and it would be tricky to extend it ?

@soronpo
Copy link
Contributor

soronpo commented May 2, 2023

Not part of the compiler team, but from what I understand, this is not something that is possible.

@doofin
Copy link
Author

doofin commented May 15, 2023

@soronpo Just have an idea: why not get the value before compile time (maybe at macro expansion time ) and inline it when compiling ?

@smarter
Copy link
Member

smarter commented May 19, 2023

Another solution is to use the ValueOf typeclass:

class c2[i <: Int: ValueOf] { println("valueOf:" + valueOf[i]) }

@doofin
Copy link
Author

doofin commented May 20, 2023

@smarter thanks! this looks interesting and almost same as what I think, but it will give errors:

No singleton value available for Int; eligible singleton types for ValueOf synthesis include literals and stable paths.

@smarter
Copy link
Member

smarter commented May 20, 2023

What does the use-site look like? If it's new c2[2] it should work, if it's a method parameter then you'll need to propagate the typeclass constraint:

def foo[i <: Int: ValueOf] = new c2[i]

@doofin
Copy link
Author

doofin commented May 21, 2023

@smarter thanks! this actually works, it's just my typo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label
Projects
None yet
Development

No branches or pull requests

3 participants