This page is no longer maintained — Please continue to the home page at www.scala-lang.org

What is the "object X" type?

12 replies
Jesper Nordenberg
Joined: 2008-12-27,
User offline. Last seen 42 years 45 weeks ago.

This weirdness was discovered after a discussion on the IRC channel:

scala> object X
defined module X

scala> val a = X
a: X.type = X$@1d2bb9f

scala> var b = X
b: object X = X$@1d2bb9f

scala> var c : X.type = X
c: X.type = X$@1d2bb9f

scala> b = c
b: object X = X$@1d2bb9f

scala> c = b
:7: error: type mismatch;
found : object X
required: X.type
c = b
^

What exactly is the "object X" type and how does it relate to "X.type"?

/Jesper Nordenberg

Chris Lewis
Joined: 2009-04-08,
User offline. Last seen 42 years 45 weeks ago.
Re: What is the "object X" type?

I'm not sure what the type member is, but:

object X

defines a singleton object X of type X (that is, a class named X is
created as a result of defining the singleton)

Jesper Nordenberg wrote:
> This weirdness was discovered after a discussion on the IRC channel:
>
> scala> object X
> defined module X
>
> scala> val a = X
> a: X.type = X$@1d2bb9f
>
> scala> var b = X
> b: object X = X$@1d2bb9f
>
> scala> var c : X.type = X
> c: X.type = X$@1d2bb9f
>
> scala> b = c
> b: object X = X$@1d2bb9f
>
> scala> c = b
> :7: error: type mismatch;
> found : object X
> required: X.type
> c = b
> ^
>
> What exactly is the "object X" type and how does it relate to "X.type"?
>
> /Jesper Nordenberg
>
>

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: What is the "object X" type?

On Wed, Apr 15, 2009 at 10:22 PM, Jesper Nordenberg wrote:
> What exactly is the "object X" type and how does it relate to "X.type"?

It's the type of which X is the singleton instance.

More formally it's the least anonymous non-singleton type of X such
that X.type is a subtype of it.

Cheers,

Miles

Jesper Nordenberg
Joined: 2008-12-27,
User offline. Last seen 42 years 45 weeks ago.
Re: What is the "object X" type?

Miles Sabin wrote:
> On Wed, Apr 15, 2009 at 10:22 PM, Jesper Nordenberg wrote:
>> What exactly is the "object X" type and how does it relate to "X.type"?
>
> It's the type of which X is the singleton instance.

Obviously X is a singleton instance of X.type as well.

> More formally it's the least anonymous non-singleton type of X such
> that X.type is a subtype of it.

Ok, but:

- Why is it needed if X.type is the only subtype and X is the only
instance of X.type?

- Why is x inferred to have type "object X" in "var x = X" and "X.type"
in "val x = X"?

- Why is "object X" not a valid type expression in Scala? I thought one
of the design objectives of Scala is to only have types that can be
specified in code.

/Jesper Nordenberg

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: Re: What is the "object X" type?

On Wed, Apr 15, 2009 at 10:46 PM, Jesper Nordenberg wrote:
> Miles Sabin wrote:
> - Why is it needed if X.type is the only subtype and X is the only instance
> of X.type?

What advantage would there be in not having it?

> - Why is x inferred to have type "object X" in "var x = X" and "X.type" in
> "val x = X"?

Because val x of X.type is by definition non-null, whereas var x of
type object X can be null, ie. type object X has more inhabitants than
X.type so they can't be identical.

> - Why is "object X" not a valid type expression in Scala? I thought one of
> the design objectives of Scala is to only have types that can be specified
> in code.

I think Martin is planning to introduce a way of specifying object
types at some point.

Cheers,

Miles

Jesper Nordenberg
Joined: 2008-12-27,
User offline. Last seen 42 years 45 weeks ago.
Re: What is the "object X" type?

Miles Sabin wrote:
> On Wed, Apr 15, 2009 at 10:46 PM, Jesper Nordenberg wrote:
>> Miles Sabin wrote:
>> - Why is it needed if X.type is the only subtype and X is the only instance
>> of X.type?
>
> What advantage would there be in not having it?

It would simplify things and there would be no need to introduce object
type syntax.

>> - Why is x inferred to have type "object X" in "var x = X" and "X.type" in
>> "val x = X"?
>
> Because val x of X.type is by definition non-null, whereas var x of
> type object X can be null, ie. type object X has more inhabitants than
> X.type so they can't be identical.

Incorrect, "val a : X.type = null" is perfectly valid code, so the set
of values for "object X" and "X.type" should be identical (null and X).

/Jesper Nordenberg

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Re: What is the "object X" type?

On Wed, Apr 15, 2009 at 11:31:32PM +0100, Miles Sabin wrote:
> > - Why is x inferred to have type "object X" in "var x = X" and "X.type" in
> > "val x = X"?
>
> Because val x of X.type is by definition non-null, whereas var x of
> type object X can be null, ie. type object X has more inhabitants than
> X.type so they can't be identical.

Are you saying null is not a member of X.type? Because that does not
seem to be the case.

scala> val x: X.type = null
x: X.type = null

scala> def foo(x: X.type) = "ok"
foo: (X.type)java.lang.String

scala> foo(null)
res2: java.lang.String = ok

If you're saying that "val x" cannot be null because it has already been
assigned a non-null value, then why wouldn't the same logic should apply
to everything else, i.e.

val x1 = "abc"
var x2: String = _

Clearly x1 does not have the same set of inhabitants as x2 (not only
can x1 not be null, it can't be anything but "abc") yet I'm pretty sure
those are both Strings.

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: Re: What is the "object X" type?

On Thu, Apr 16, 2009 at 12:11 AM, Paul Phillips wrote:
> On Wed, Apr 15, 2009 at 11:31:32PM +0100, Miles Sabin wrote:
>> > - Why is x inferred to have type "object X" in "var x = X" and "X.type" in
>> > "val x = X"?
>>
>> Because val x of X.type is by definition non-null, whereas var x of
>> type object X can be null, ie. type object X has more inhabitants than
>> X.type so they can't be identical.
>
> Are you saying null is not a member of X.type? Because that does not
> seem to be the case.
>
> scala> val x: X.type = null
> x: X.type = null

Yup, my mistake.

Nevertheless there's a distinction between a singleton type and a
non-singleton which matters: singleton types are related to stable
types why play a role in paths ... see SLS 3.2.1 and 6.4.

Cheers,

Miles

Jesper Nordenberg
Joined: 2008-12-27,
User offline. Last seen 42 years 45 weeks ago.
Re: What is the "object X" type?

Miles Sabin wrote:
> Nevertheless there's a distinction between a singleton type and a
> non-singleton which matters: singleton types are related to stable
> types why play a role in paths ... see SLS 3.2.1 and 6.4.

I fail to see how this has any relevance. There are no values of type
"object X" and the only subtype is "X.type". What possible benefits
exists to have the "object X" type?

This is the only case I know of where the inferred type of a var and val
with the same initializer expression differs.

/Jesper Nordenberg

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: Re: What is the "object X" type?

On Thu, Apr 16, 2009 at 8:21 AM, Jesper Nordenberg wrote:
> I fail to see how this has any relevance. There are no values of type
> "object X" and the only subtype is "X.type".

Yes there are: X, and as you and Paul have pointed out, null.

And if "object X" becomes specifiable there could be arbitrarily more, eg.,

object X
object Y extends [object X] // Not current Scala

Nb. that this would make no sense at all if "object X" were the singelton type.

Cheers,

Miles

Jesper Nordenberg
Joined: 2008-12-27,
User offline. Last seen 42 years 45 weeks ago.
Re: What is the "object X" type?

Miles Sabin wrote:
> And if "object X" becomes specifiable there could be arbitrarily more, eg.,
>
> object X
> object Y extends [object X] // Not current Scala
>
> Nb. that this would make no sense at all if "object X" were the singelton type.

Yes, in this case it would make sense to have the "object X" type.
However, this doesn't explain why var is inferred to type "object X" and
val to "X.type". For consistency val should be inferred to type "object
X". There are no other cases where the type of a val is inferred to a
singleton type (for example, compare to "val x = 10" or "val x = new
Object").

/Jesper Nordenberg

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: Re: What is the "object X" type?

On Thu, Apr 16, 2009 at 9:14 AM, Jesper Nordenberg wrote:
> Yes, in this case it would make sense to have the "object X" type. However,
> this doesn't explain why var is inferred to type "object X" and val to
> "X.type". For consistency val should be inferred to type "object X".

OK, but now see 3.2.1 and 6.4 ...

Cheers,

Miles

malinova
Joined: 2010-11-01,
User offline. Last seen 1 year 48 weeks ago.
<a href="http:/

Эти обезьяны стали гордостью моей коллекции, и несколько дней я наслаждался своей радостью Затем в одно злополучное утро какой-то отвратительный мальчишка (я искренне верю, что он дурно кончит) пробрался в мой зверинец и начал открывать клетки, чтобы покормить обезьян

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland