-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Dotty doesn't always detect Constants #8714
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
Labels
Comments
The specification
|
Now the following code inlines all the inline vals and generates not fields/getters for them. import scala.util.Random
object ConstantTester {
val byteConstant: Byte = 0
val intConstant: Int = 1
val untypedConstant = 2
// inline val inlinedByteConstant: Byte = 3
// inline val inlinedIntConstant: Int = 4
inline val inlinedUntypedConstant = 5
private val privateByteConstant: Byte = 0
private val privateIntConstant: Int = 1
private val privateUntypedConstant = 2
// inline private val privateInlinedByteConstant: Byte = 3
// inline private val privateInlinedIntConstant: Int = 4
inline private val privateInlinedUntypedConstant = 5
def main(args: Array[String]): Unit = {
val int1 = byteConstant + intConstant + untypedConstant
val int2 = 1 + 2 + inlinedUntypedConstant
val int3 = privateByteConstant + privateIntConstant + privateUntypedConstant
val int4 = 2 + 3 + privateInlinedUntypedConstant
}
} package <empty> {
@scala.annotation.internal.SourceFile("Foo.scala") final module class
ConstantTester$
extends Object, java.io.Serializable {
def <init>(): Unit =
{
super()
ConstantTester.byteConstant = 0
ConstantTester.intConstant = 1
ConstantTester.untypedConstant = 2
ConstantTester.privateByteConstant = 0
ConstantTester.privateIntConstant = 1
ConstantTester.privateUntypedConstant = 2
()
}
private def writeReplace(): Object =
new scala.runtime.ModuleSerializationProxy(classOf[ConstantTester$])
private val byteConstant: Byte
def byteConstant(): Byte = ConstantTester.byteConstant
private val intConstant: Int
def intConstant(): Int = ConstantTester.intConstant
private val untypedConstant: Int
def untypedConstant(): Int = ConstantTester.untypedConstant
private val privateByteConstant: Byte
private val privateIntConstant: Int
private val privateUntypedConstant: Int
def main(args: String[]): Unit =
{
val int1: Int =
ConstantTester.byteConstant().+(ConstantTester.intConstant()).+(
ConstantTester.untypedConstant()
)
val int2: Int = 8
val int3: Int =
ConstantTester.privateByteConstant.+(ConstantTester.privateIntConstant
)
.+(ConstantTester.privateUntypedConstant)
val int4: Int = 10
()
}
}
final lazy module val ConstantTester: ConstantTester$ = new ConstantTester$()
} |
Thanks, looking forward to trying this out in the next release! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wrote a test program that tries out four of defining a constant on an Object:
I also tried typing the constants as Int and as Byte, as well as omitting the type altogether.
Minimized code
Output
$ ~/bin/dotty-0.23.0-RC1/bin/dotc test.scala
failure to convert Constant(true) to TypeRef(ThisType(TypeRef(NoPrefix,module class scala)),class Byte)
I then ran the generated .class through Luyten decompiler. It turns out that depending on how I declared the value, dotc will do one of five things:
Expectation
I had a few expectations that were broken:
The text was updated successfully, but these errors were encountered: