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

Re: Why are there no objects for `true` and `false`?

10 replies
Roland Kuhn 3
Joined: 2011-07-23,
User offline. Last seen 42 years 45 weeks ago.

On Jul 24, 2011, at 00:06 , Antoras wrote:

> Am Sa 23 Jul 2011 23:42:52 CEST, Roland Kuhn schrieb:
>> or, to put it slightly differently: false and true are treated basically the same way as 0 and 1. There are 8 non-object types on the JVM which receive special treatment, so the Java interop argument equally applies to all of them.
>
> That is true, but I don't understand why `true` and `false` should also be written as the same as in Java. In Scala they can be represented by objects, not classes.

What exactly would you gain in making them non-primitive? I never felt like I needed that “feature”.

Ah, I see now: you think they are represented by some class? Well, they are not. There is the wrapper java.lang.Boolean, though, but you don’t really want to use boxing when it is not necessary, do you?

Regards,

Roland

Antoras
Joined: 2010-05-23,
User offline. Last seen 1 year 19 weeks ago.
Re: Why are there no objects for `true` and `false`?

Am So 24 Jul 2011 00:13:30 CEST, Roland Kuhn schrieb:
> On Jul 24, 2011, at 00:06 , Antoras wrote:
>
>> Am Sa 23 Jul 2011 23:42:52 CEST, Roland Kuhn schrieb:
>>> or, to put it slightly differently: false and true are treated basically the same way as 0 and 1. There are 8 non-object types on the JVM which receive special treatment, so the Java interop argument equally applies to all of them.
>>
>> That is true, but I don't understand why `true` and `false` should also be written as the same as in Java. In Scala they can be represented by objects, not classes.
>
> What exactly would you gain in making them non-primitive? I never felt like I needed that “feature”.
>
> Ah, I see now: you think they are represented by some class? Well, they are not. There is the wrapper java.lang.Boolean, though, but you don’t really want to use boxing when it is not necessary, do you?
>
> Regards,
>
> Roland

No, I don't want to use wrappers. I want to write them in upper-case.
The compiler should transform them implicitly to the primitive values.
With lower-case they look alien to me and not like an object.

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Why are there no objects for `true` and `false`?

val True = true
val False = false

On Jul 24, 2011 12:41 AM, "Antoras" <mail [at] antoras [dot] de> wrote:
> Am So 24 Jul 2011 00:13:30 CEST, Roland Kuhn schrieb:
>> On Jul 24, 2011, at 00:06 , Antoras wrote:
>>
>>> Am Sa 23 Jul 2011 23:42:52 CEST, Roland Kuhn schrieb:
>>>> or, to put it slightly differently: false and true are treated basically the same way as 0 and 1. There are 8 non-object types on the JVM which receive special treatment, so the Java interop argument equally applies to all of them.
>>>
>>> That is true, but I don't understand why `true` and `false` should also be written as the same as in Java. In Scala they can be represented by objects, not classes.
>>
>> What exactly would you gain in making them non-primitive? I never felt like I needed that “feature”.
>>
>> Ah, I see now: you think they are represented by some class? Well, they are not. There is the wrapper java.lang.Boolean, though, but you don’t really want to use boxing when it is not necessary, do you?
>>
>> Regards,
>>
>> Roland
>
> No, I don't want to use wrappers. I want to write them in upper-case.
> The compiler should transform them implicitly to the primitive values.
> With lower-case they look alien to me and not like an object.
Roland Kuhn 3
Joined: 2011-07-23,
User offline. Last seen 42 years 45 weeks ago.
Re: Why are there no objects for `true` and `false`?
Or, soon:
val True: true.type = trueval False: false.type = false
On Jul 24, 2011, at 00:59 , √iktor Ҡlang wrote:

val True = true
val False = false

On Jul 24, 2011 12:41 AM, "Antoras" <mail [at] antoras [dot] de> wrote:
> Am So 24 Jul 2011 00:13:30 CEST, Roland Kuhn schrieb:
>> On Jul 24, 2011, at 00:06 , Antoras wrote:
>>
>>> Am Sa 23 Jul 2011 23:42:52 CEST, Roland Kuhn schrieb:
>>>> or, to put it slightly differently: false and true are treated basically the same way as 0 and 1. There are 8 non-object types on the JVM which receive special treatment, so the Java interop argument equally applies to all of them.
>>>
>>> That is true, but I don't understand why `true` and `false` should also be written as the same as in Java. In Scala they can be represented by objects, not classes.
>>
>> What exactly would you gain in making them non-primitive? I never felt like I needed that “feature”.
>>
>> Ah, I see now: you think they are represented by some class? Well, they are not. There is the wrapper java.lang.Boolean, though, but you don’t really want to use boxing when it is not necessary, do you?
>>
>> Regards,
>>
>> Roland
>
> No, I don't want to use wrappers. I want to write them in upper-case.
> The compiler should transform them implicitly to the primitive values.
> With lower-case they look alien to me and not like an object.

Antoras
Joined: 2010-05-23,
User offline. Last seen 1 year 19 weeks ago.
Re: Why are there no objects for `true` and `false`?

On 24.07.2011 00:59, √iktor Ҡlang wrote:
>
> val True = true
> val False = false
>
Yes, it is easy to avoid the lower-case symbols. But that doesn't answer
my question why it is not defined in Predef or directly by the Scala-spec.

Maybe it is, as Alex Repain said, due to Java-interoperability.

jibal
Joined: 2010-12-01,
User offline. Last seen 1 year 45 weeks ago.
Re: Why are there no objects for `true` and `false`?

On Sat, Jul 23, 2011 at 4:12 PM, Antoras wrote:
> On 24.07.2011 00:59, √iktor Ҡlang wrote:
>>
>> val True = true
>> val False = false
>>
> Yes, it is easy to avoid the lower-case symbols. But that doesn't answer my
> question why it is not defined in Predef or directly by the Scala-spec.

Perhaps that's because you didn't ask that question previously. You
wrote "I want to write them in upper-case", and you've been shown how
your wish can be granted.

> Maybe it is, as Alex Repain said, due to Java-interoperability.

Or maybe it's just one of those things that happens during the
imperfect evolution of a programming language. Consider submitting an
enhancement request.

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: Why are there no objects for `true` and `false`?
On Sat, Jul 23, 2011 at 6:39 PM, Antoras <mail [at] antoras [dot] de> wrote:
No, I don't want to use wrappers. I want to write them in upper-case. The compiler should transform them implicitly to the primitive values. With lower-case they look alien to me and not like an object.

They are not objects.  They are primitive data types.  'x' doesn't look like an object to me either.  Nor does 0.771.  I'd rather not write Charx and ZeroPointSevenSevenOne either, thanks.

  --Rex

Stefan Langer
Joined: 2009-10-23,
User offline. Last seen 42 years 45 weeks ago.
Re: Why are there no objects for `true` and `false`?

If it behaves like a Boolean and it looks like a Boolean it must be a Boolean.

Welcome to Scala version 2.9.0.1 (OpenJDK 64-Bit Server VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.

scala> true.toString
res0: java.lang.String = true

scala> true.hashCode
res1: Int = 1231

scala> true.equals(false)
res2: Boolean = false

scala> true.toInt
:8: error: value toInt is not a member of Boolean
true.toInt

So it doesn't really matter whether they are capitalized or not. true
and false behave like Boolean.

-
Stefan

2011/7/24 Rex Kerr :
> On Sat, Jul 23, 2011 at 6:39 PM, Antoras wrote:
>>
>> No, I don't want to use wrappers. I want to write them in upper-case. The
>> compiler should transform them implicitly to the primitive values. With
>> lower-case they look alien to me and not like an object.
>
> They are not objects.  They are primitive data types.  'x' doesn't look like
> an object to me either.  Nor does 0.771.  I'd rather not write Charx and
> ZeroPointSevenSevenOne either, thanks.
>
>   --Rex
>
>

DaveScala
Joined: 2011-03-18,
User offline. Last seen 1 year 21 weeks ago.
Re: Why are there no objects for `true` and `false`?

Everything in Scala is an object (including numerical values and
functions) and all values are instances of a class:
See:
http://www.scala-lang.org/node/128

Seth Tisue
Joined: 2008-12-16,
User offline. Last seen 34 weeks 3 days ago.
Re: Re: Why are there no objects for `true` and `false`?

On Sun, Jul 24, 2011 at 10:44 AM, Dave wrote:
> Everything in Scala is an object (including numerical values and
> functions) and all values are instances of a class

With regards to this, a very pleasant improvement to getClass was made
on trunk since 2.9 was
released:

Welcome to Scala version 2.9.0.1

scala> true.getClass
:8: error: type mismatch;
found : Boolean(true)
required: ?{val getClass: ?}

Welcome to Scala version 2.10.0.r25323-b20110719121652

scala> true.getClass
res1: java.lang.Class[Boolean] = boolean

This was fixed by Paul Phillips in r25137, closing SI-496 and SI-864.

Antoras
Joined: 2010-05-23,
User offline. Last seen 1 year 19 weeks ago.
Re: Re: Why are there no objects for `true` and `false`?

Am So 24 Jul 2011 18:07:04 CEST, Seth Tisue schrieb:
> On Sun, Jul 24, 2011 at 10:44 AM, Dave wrote:
>> Everything in Scala is an object (including numerical values and
>> functions) and all values are instances of a class
>
> With regards to this, a very pleasant improvement to getClass was made
> on trunk since 2.9 was
> released:
>
> Welcome to Scala version 2.9.0.1
>
> scala> true.getClass
> :8: error: type mismatch;
> found : Boolean(true)
> required: ?{val getClass: ?}
>
> Welcome to Scala version 2.10.0.r25323-b20110719121652
>
> scala> true.getClass
> res1: java.lang.Class[Boolean] = boolean
>
> This was fixed by Paul Phillips in r25137, closing SI-496 and SI-864.

That is interesting. Thanks for sharing.

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