- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
specifying namespaces
Tue, 2010-11-09, 21:12
Would anyone like to comment on the accuracy or lack thereof found in
the comment header at bottom? Especially the last three lines?
Why does this come up? I am fixing up the command line options which do
nice things like show you the members of something specific. Let's say
you are compiling this:
class Gox {
object Zed { def f1: Int = 5 }
class Zed { def f2: Int = 10 }
}
You are interested only in object Zed. Clearly object Zed is a
particular entity with a particular set of members. Its existence has
meaning outside of specific instances of Gox. How to refer to that
identity? Even if one cannot refer to it from scala source, one should
be able to speak of it. The obvious
Gox#Zed.type
does not work in scala source, although that is not an obstacle here
since I'm only looking for a way for people to say what they mean
without having to specify things beyond the name. Is it ambiguous
somehow? (It's not as if it could be parsed (Gox#Zed).type...) Is there
another candidate?
/** Translate a String into a list of simple TypeNames and TermNames.
* In all segments before the last, type/term is determined by whether
* the following separator char is '.' or '#'. In the last segment,
* the argument "assumeTerm" determines it. Examples:
*
* package foo {
* object Lorax { object Wog ; class Wog }
* class Lorax { object Zax ; class Zax }
* }
*
* f("foo.Lorax", true) == List("foo": Term, "Lorax": Term) // object Lorax
* f("foo.Lorax", false) == List("foo": Term, "Lorax": Type) // class Lorax
* f("Lorax.Wog", true) == List("Lorax": Term, "Wog": Term) // object Wog
* f("Lorax.Wog", false) == List("Lorax": Term, "Wog": Type) // class Wog
* f("Lorax#Zax", true) == List("Lorax": Type, "Zax": Term) // object Zax
* f("Lorax#Zax", false) == List("Lorax": Type, "Zax": Type) // class Zax
*
* Note that in actual scala syntax you cannot refer to object Zax without an
* instance of Lorax, so Lorax#Zax could only mean the type. One might think
* that Lorax#Zax.type would work, but this is not accepted by the parser.
*/
Tue, 2010-11-09, 22:47
#2
Re: specifying namespaces
I had what I thought would be a brilliant suggestion except, Something strange happened.
scala> type GoxZed = t.Zed forSome { val t : Gox } defined type alias GoxZed
scala> val x = new Gox x: Gox = Gox@500ab58d
scala> val y : GoxZed = x.Zed java.lang.ClassCastException: Gox$Zed$ cannot be cast to Gox$Zed
It seems the existential is the easiest way to refer to the member Zed on a value of type Gox. However, it looks like the wrong class cast operation is being compiled, even though the types are satisfied. Is this a known bug?
- Josh
On Tue, Nov 9, 2010 at 3:46 PM, martin odersky <martin.odersky@epfl.ch> wrote:
scala> type GoxZed = t.Zed forSome { val t : Gox } defined type alias GoxZed
scala> val x = new Gox x: Gox = Gox@500ab58d
scala> val y : GoxZed = x.Zed java.lang.ClassCastException: Gox$Zed$ cannot be cast to Gox$Zed
It seems the existential is the easiest way to refer to the member Zed on a value of type Gox. However, it looks like the wrong class cast operation is being compiled, even though the types are satisfied. Is this a known bug?
- Josh
On Tue, Nov 9, 2010 at 3:46 PM, martin odersky <martin.odersky@epfl.ch> wrote:
On Tue, Nov 9, 2010 at 9:12 PM, Paul Phillips <paulp@improving.org> wrote:
Would anyone like to comment on the accuracy or lack thereof found inAnd rightly so, because in X.type, X is required to be a path. The comment seems accurate to me.
the comment header at bottom? Especially the last three lines?
Why does this come up? I am fixing up the command line options which do
nice things like show you the members of something specific. Let's say
you are compiling this:
class Gox {
object Zed { def f1: Int = 5 }
class Zed { def f2: Int = 10 }
}
You are interested only in object Zed. Clearly object Zed is a
particular entity with a particular set of members. Its existence has
meaning outside of specific instances of Gox. How to refer to that
identity? Even if one cannot refer to it from scala source, one should
be able to speak of it. The obvious
Gox#Zed.type
does not work in scala source, although that is not an obstacle here
since I'm only looking for a way for people to say what they mean
without having to specify things beyond the name. Is it ambiguous
somehow? (It's not as if it could be parsed (Gox#Zed).type...) Is there
another candidate?
/** Translate a String into a list of simple TypeNames and TermNames.
* In all segments before the last, type/term is determined by whether
* the following separator char is '.' or '#'. In the last segment,
* the argument "assumeTerm" determines it. Examples:
*
* package foo {
* object Lorax { object Wog ; class Wog }
* class Lorax { object Zax ; class Zax }
* }
*
* f("foo.Lorax", true) == List("foo": Term, "Lorax": Term) // object Lorax
* f("foo.Lorax", false) == List("foo": Term, "Lorax": Type) // class Lorax
* f("Lorax.Wog", true) == List("Lorax": Term, "Wog": Term) // object Wog
* f("Lorax.Wog", false) == List("Lorax": Term, "Wog": Type) // class Wog
* f("Lorax#Zax", true) == List("Lorax": Type, "Zax": Term) // object Zax
* f("Lorax#Zax", false) == List("Lorax": Type, "Zax": Type) // class Zax
*
* Note that in actual scala syntax you cannot refer to object Zax without an
* instance of Lorax, so Lorax#Zax could only mean the type. One might think
* that Lorax#Zax.type would work, but this is not accepted by the parser.
*/
Cheers
-- Martin
Tue, 2010-11-09, 23:47
#3
Re: specifying namespaces
On Tue, Nov 09, 2010 at 04:38:34PM -0500, Josh Suereth wrote:
> scala> type GoxZed = t.Zed forSome { val t : Gox }
> defined type alias GoxZed
>
> scala> val x = new Gox
> x: Gox = Gox@500ab58d
>
> scala> val y : GoxZed = x.Zed
> java.lang.ClassCastException: Gox$Zed$ cannot be cast to Gox$Zed
Oh, you were mighty close. You did point the way. It's not t.Zed
forSome t, it's t.Zed.type.
class Gox {
object Zed { def f1: Int = 5 }
class Zed { def f2: Int = 10 }
}
object Test {
type GoxZed = t.Zed.type forSome { val t : Gox }
var gz: GoxZed = null
def main(args: Array[String]): Unit = {
val x1 = new Gox
gz = x1.Zed
val x2 = new Gox
gz = x2.Zed
}
}
It works. The class cast exception is arising because you are claiming
it is type Gox#Zed and giving it Gox#Zed.type. Although I'm not sure
why that doesn't manifest as a type error.
Wed, 2010-11-10, 00:07
#4
Re: specifying namespaces
t#Zed forSome { val t : Gox } will not compile,
But the version i wrote.... Well i can't recall the type rewriting rules off the top of my head to know... But i think it is type sound and a bug....
Any expert commentary?
In any case, glad to see it worked eventually!
On Nov 9, 2010 5:38 PM, "Paul Phillips" <paulp@improving.org> wrote:On Tue, Nov 09, 2010 at 04:38:34PM -0500, Josh Suereth wrote:
Oh, you were mighty close. You did point the way. It's not t.Zed
> scala> type GoxZed = t.Zed forSome {...
forSome t, it's t.Zed.type.
object Test {
class Gox {
object Zed { def f1: Int = 5 }
class Zed { def f2: Int = 10 }
}
type GoxZed = t.Zed.type forSome { val t : Gox }
var gz: GoxZed = null
def main(args: Array[String]): Unit = {
val x1 = new Gox
gz = x1.Zed
val x2 = new Gox
gz = x2.Zed
}
}
It works. The class cast exception is arising because you are claiming
it is type Gox#Zed and giving it Gox#Zed.type. Although I'm not sure
why that doesn't manifest as a type error.
--
Paul Phillips | Atheists dig the best foxholes.
Apatheist |
Empiricist |
all hip pupils! |----------* http://www.improving.org/paulp/ *----------
Wed, 2010-11-10, 00:27
#5
Re: specifying namespaces
Nevermind. ... You are correct. It probably shouldn't compile.. I'll respond after giving appropriate thought next time.
Also, typo in previous email, i meant t.type#Zed
On Nov 9, 2010 6:05 PM, "Josh Suereth" <joshua.suereth@gmail.com> wrote:
t#Zed forSome { val t : Gox } will not compile,
But the version i wrote.... Well i can't recall the type rewriting rules off the top of my head to know... But i think it is type sound and a bug....
Any expert commentary?
In any case, glad to see it worked eventually!
>
> On Nov 9, 2010 5:38 PM, "Paul Phillips" <paulp@improving.org> wrote:
>> On Tue, Nov 09, 2010 at 04:38:34PM -0500, Josh Suereth wrote:
> scala> type GoxZed = t.Zed forSome {...
>
> Oh, you were mighty close. You did point the way. It's not t.Zed
> forSome t, it's t.Zed.type...
Wed, 2010-11-10, 00:37
#6
Re: specifying namespaces
On Tue, Nov 09, 2010 at 04:38:34PM -0500, Josh Suereth wrote:
> scala> val y : GoxZed = x.Zed
> java.lang.ClassCastException: Gox$Zed$ cannot be cast to Gox$Zed
https://lampsvn.epfl.ch/trac/scala/ticket/3987
"namespace blur results in unsoundness"
My kids will probably grow up to be huge fans of Namespace Blur and
their confusing music.
On Tue, Nov 9, 2010 at 9:12 PM, Paul Phillips <paulp@improving.org> wrote:
And rightly so, because in X.type, X is required to be a path. The comment seems accurate to me.
Cheers
-- Martin