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

Why does Predef have "type String = java.lang.String", etc.?

2 replies
Dean Wampler
Joined: 2008-12-26,
User offline. Last seen 42 years 45 weeks ago.
... instead of just using the fact that String can be used without qualification since the parser does an implicit "java.lang._" import? Is there a specific advantage to declaring the types vs. relying on the implicit imports? Or, is it possibly true that java.lang._ and scala._ aren't actually imported automatically, contrary to what I've read, so these definitions really are necessary?
Thanks in advance.

dean

--
Dean Wampler
twitter: @deanwampler, @chicagoscala
Chicago-Area Scala Enthusiasts (CASE):
-  http://groups.google.com/group/chicagoscala
-  http://www.meetup.com/chicagoscala/ (Meetings)
http://www.objectmentor.com
http://www.polyglotprogramming.com
http://www.aspectprogramming.com
http://aquarium.rubyforge.org
http://www.contract4j.org
etorreborre
Joined: 2008-09-03,
User offline. Last seen 1 year 22 weeks ago.
Re: Why does Predef have "type String = java.lang.String", etc.

Hi Dean,

I tried to do some experiment in the Scala console:

import Predef.{String => Renamed}
val s: String = "hello"
val s2: Renamed = "hello"
val s3 = new java.lang.String("hello")
val p: Process = null

And I get:

import Predef.{String=>Renamed}
s: String = hello
s2: String = hello
s3: java.lang.String = hello
p: java.lang.Process = null

So it seems that java.lang._ is imported automatically and even if you
renamed the "String" type alias for java.lang.String, you still can access
it.

Eric.

Iulian Dragos
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Why does Predef have "type String = java.lang.String", etc

It's to have a platform independent String, and the like. The standard
library for MSIL has a different type alias for String. Once the MSIL
backend is back on track (and we have someone actively working on
this), these aliases will have a more substantial motivation.

iulian

On Wed, Mar 25, 2009 at 6:07 AM, Eric Torreborre wrote:
>
> Hi Dean,
>
> I tried to do some experiment in the Scala console:
>
> import Predef.{String => Renamed}
> val s: String = "hello"
> val s2: Renamed = "hello"
> val s3 = new java.lang.String("hello")
> val p: Process = null
>
> And I get:
>
> import Predef.{String=>Renamed}
> s: String = hello
> s2: String = hello
> s3: java.lang.String = hello
> p: java.lang.Process = null
>
> So it seems that java.lang._ is imported automatically and even if you
> renamed the "String" type alias for java.lang.String, you still can access
> it.
>
> Eric.
> --
> View this message in context: http://www.nabble.com/-scala--Why-does-Predef-have-%22type-String-%3D-ja...
> Sent from the Scala mailing list archive at Nabble.com.
>
>

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