You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If generate a singleton type of val, the constructor of the type of immutable variable is called.
If generate a singleton type of object, another object is generated.
Since they are separate instances, false will be returned when comparing reference by eq method.
# scalac GenSingleton.scala
# scala GenSingleton
Exception in thread "main" java.lang.IllegalAccessError: class GenSingleton$ tried to access private method 'void MyObj$.<init>()' (GenSingleton$ and MyObj$ are in unnamed module of loader 'app')
at GenSingleton$.main(GenSingleton.scala:9)
at GenSingleton.main(GenSingleton.scala)
Expectation
I expect that the singleton type cannot be generated by new.
It seems that generation is allowed from 3.0.0-RC2 and later, and it is a compile error 3.0.0-RC1 and earlier.
# scala -version
Starting scala3 REPL...
Scala compiler version 3.0.0-RC1--Copyright2002-2021, LAMP/EPFLvalhello:String="hello"objectMyObj {
vala:Int=123valb:Double=456.789valc:String="ABC"
}
scala>valstringFromSingleton:String=new hello.type()
1|valstringFromSingleton:String=new hello.type()
|^^^^^^^^^^| (hello : String) is not a classtype
scala>valmyObjFromSingleton:MyObj.type=newMyObj.type()
1|valmyObjFromSingleton:MyObj.type=newMyObj.type()
|^^^^^^^^^^|MyObj.type is not a classtype
The text was updated successfully, but these errors were encountered:
Uh oh!
There was an error while loading. Please reload this page.
Compiler version
3.0.0
Minimized code
Output
If generate a singleton type of
val
, the constructor of the type of immutable variable is called.If generate a singleton type of
object
, anotherobject
is generated.Since they are separate instances,
false
will be returned when comparing reference byeq
method.In addition, for non-REPL, creating a
object
resulted in ajava.lang.IllegalAccessError
.Expectation
I expect that the singleton type cannot be generated by
new
.It seems that generation is allowed from
3.0.0-RC2
and later, and it is a compile error3.0.0-RC1
and earlier.The text was updated successfully, but these errors were encountered: