According to the Scala Language Specification, packages are AnyRef values and have types. How does this make sense? -
i reading through scala language specification , have encountered confusing, namely, implication packages values , have types.
here how concluded (maybe incorrectly?) unusual fact scala language specification:
background:
in type designators section, written :
simpletype ::= stableid
a type designator refers named value type. can simple or qualified. such type designators shorthands type projections.
specifically, unqualified type name t t bound in class, object, or package c taken shorthand c.this.type#t. if t not bound in class, object, or package, t taken shorthand ε.type#t.
a qualified type designator has form p.t p path , t type name. such type designator equivalent type projection p.type#t.
some type designators , expansions listed below. assume local type parameter t, value maintable type fellow member node , standard class scala.int,
furthermore, considering type projection definition:
simpletype ::= simpletype ‘#’ id
a type projection t#x references type fellow member named x of type t.
lastly, singleton type definition says :
simpletype ::= path ‘.’ type
a singleton type of form p.type, p path pointing value expected conform scala.anyref. type denotes set of values consisting of null , value denoted p.
chain of reasoning:
so, know :
1) scala
in scala.int
package.
2) scala.int
syntactic sugar scala.type#int
(as explained in type designator definition , shown in image above)
3) scala.type#int
type projection, scala.type
must type according the type projection definition states:
a type projection t#x references type fellow member named x of type t.
4) scala.type
type ! namely, singleton type, according definition of singleton types, says :
a singleton type of form p.type, p path pointing value expected conform scala.anyref.
5) scala
corresponds p
value conforming anyref
6) in scala language specification here written that:
every value in scala has type...
7) bundle scala
has type.
questions:
1) reasoning correct? bundle scala
value conforming anyref ? if reasoning not correct, please explain why.
assuming above reasoning right , bundle scala
indeed value:
2) in sense bundle scala
value ? how create sense ? in context can think of scala
value in same way think of 5:int
beingness value 5
type int
?
3) if bundle scala
value conforms anyref, should able set value variable, can that, if not, why not ?
4) how value of bundle scala
represented internally (by compiler) behind scenes ? object ? value nowadays @ runtime in form of jvm object ? if yes, how can hold of ? how can phone call tostring
method on ?
based on experimentation rather specification:
1) packages have types conform any
, not anyref
. can't assign them type
s:
scala> type t = java.type <console>:7: error: type mismatch; found : java.type required: anyref note java extends any, not anyref. such types can participate in value classes, instances cannot appear in singleton types or in reference comparisons.
interestingly not general restriction on any
types:
scala> type q = int defined type alias q
i suspect else going on
2) suspect reason in spec back upwards package object
s. if write
package object foo { val x = 5 val y = }
then weird foo
isn't value (and weird y
isn't value). , if ordinary bundle magically sprouted value defined package object
it, weird.
3) can't see way it, because can't see way access value. error says bundle not value:
val f = foo test.scala:10: bundle foo not value
it may value foo
"exists" in sense, there's no way name in source (except within bundle object itself)?
scala specifications language-specifications
No comments:
Post a Comment