What to expect from println

View: New views
8 Messages — Rating Filter:   Alert me  

What to expect from println

by jlist9 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all, please bear with me for my naive questions.

I have this code:

    val list = List(1, 2, 3)
    println(list.toString())
    println(list)

    var arry = Array(1, 2, 3)
    println(arry.toString())
    println(arry)

When it runs it outputs:

List(1, 2, 3)
List(1, 2, 3)
Array(1, 2, 3)
[I@a981ca

So it looks like println on a List object calls the List.toString( )
method while
println on an Array object does not call the Array.toString() method. Any
explanation to this difference?

Re: What to expect from println

by Paul Phillips-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Oct 29, 2009 at 03:46:04PM -0700, jlist9 wrote:
> So it looks like println on a List object calls the List.toString( )
> method while println on an Array object does not call the
> Array.toString() method. Any explanation to this difference?

Arrays used to have a whole lot of leaky cleverness going on.  You'll be
happy to know that in 2.8 both the Array printlns print the funny
squiggles, although the repl still makes a half-hearted effort to print
something you can understand.

--
Paul Phillips      | Before a man speaks it is always safe to assume
Caged Spirit       | that he is a fool.  After he speaks, it is seldom
Empiricist         | necessary to assume it.
up hill, pi pals!  |     -- H. L. Mencken

Re: What to expect from println

by jlist9 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hmm. I just tried the latest 2.8 and the behavior seems to be the same
as in 2.7.7.
Is there a reason why println doesn't call the .toString() method on an object?

On Thu, Oct 29, 2009 at 4:13 PM, Paul Phillips <paulp@...> wrote:

> On Thu, Oct 29, 2009 at 03:46:04PM -0700, jlist9 wrote:
>> So it looks like println on a List object calls the List.toString( )
>> method while println on an Array object does not call the
>> Array.toString() method. Any explanation to this difference?
>
> Arrays used to have a whole lot of leaky cleverness going on.  You'll be
> happy to know that in 2.8 both the Array printlns print the funny
> squiggles, although the repl still makes a half-hearted effort to print
> something you can understand.
>
> --
> Paul Phillips      | Before a man speaks it is always safe to assume
> Caged Spirit       | that he is a fool.  After he speaks, it is seldom
> Empiricist         | necessary to assume it.
> up hill, pi pals!  |     -- H. L. Mencken
>

Re: What to expect from println

by Paul Phillips-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Oct 30, 2009 at 12:04:46PM -0700, jlist9 wrote:
> Hmm. I just tried the latest 2.8 and the behavior seems to be the same
> as in 2.7.7.

Try again? It isn't.  If you think otherwise, show proof.

--
Paul Phillips      | We act as though comfort and luxury were the chief
Apatheist          | requirements of life, when all that we need to make us
Empiricist         | really happy is something to be enthusiastic about.
slap pi uphill!    |     -- Charles Kingsley

Re: What to expect from println

by jlist9 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Oh in 2.8 println(a.toString()) is also giving me "[I@1945a5a".
This looks like a step backwards. Is this a bug?


------------------------------
C:\java\scala-2.7.6.final\bin>scala
Welcome to Scala version 2.7.6.final (Java HotSpot(TM) Client VM, Java
1.6.0_14).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val a = Array(1, 2, 3)
a: Array[Int] = Array(1, 2, 3)

scala> println(a)
[I@97f621

scala> val l = List(1, 2, 3)
l: List[Int] = List(1, 2, 3)

scala> println(l)
List(1, 2, 3)


On Fri, Oct 30, 2009 at 12:32 PM, Paul Phillips <paulp@...> wrote:

> On Fri, Oct 30, 2009 at 12:04:46PM -0700, jlist9 wrote:
>> Hmm. I just tried the latest 2.8 and the behavior seems to be the same
>> as in 2.7.7.
>
> Try again? It isn't.  If you think otherwise, show proof.
>
> --
> Paul Phillips      | We act as though comfort and luxury were the chief
> Apatheist          | requirements of life, when all that we need to make us
> Empiricist         | really happy is something to be enthusiastic about.
> slap pi uphill!    |     -- Charles Kingsley
>

Re: What to expect from println

by Paul Phillips-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Oct 30, 2009 at 01:52:20PM -0700, jlist9 wrote:
> This looks like a step backwards. Is this a bug?

It is not a bug.  It is the result of "calling .toString() method on an
object" as requested.  Google java arrays.

--
Paul Phillips      | Those who can make you believe absurdities
Everyman           | can make you commit atrocities.
Empiricist         |     -- Voltaire
i'll ship a pulp   |----------* http://www.improving.org/paulp/ *----------

Re: What to expect from println

by Daniel Sobral :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

No, this is Java's toString on Arrays. Previously, Scala pretended to have their own Arrays, but that turned out to be impossible to do consistently. As you have verified yourself.

Since it couldn't be done right, now Arrays are Java Arrays.

On Fri, Oct 30, 2009 at 6:52 PM, jlist9 <jlist9@...> wrote:
Oh in 2.8 println(a.toString()) is also giving me "[I@1945a5a".
This looks like a step backwards. Is this a bug?


------------------------------
C:\java\scala-2.7.6.final\bin>scala
Welcome to Scala version 2.7.6.final (Java HotSpot(TM) Client VM, Java
1.6.0_14).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val a = Array(1, 2, 3)
a: Array[Int] = Array(1, 2, 3)

scala> println(a)
[I@97f621

scala> val l = List(1, 2, 3)
l: List[Int] = List(1, 2, 3)

scala> println(l)
List(1, 2, 3)


On Fri, Oct 30, 2009 at 12:32 PM, Paul Phillips <paulp@...> wrote:
> On Fri, Oct 30, 2009 at 12:04:46PM -0700, jlist9 wrote:
>> Hmm. I just tried the latest 2.8 and the behavior seems to be the same
>> as in 2.7.7.
>
> Try again? It isn't.  If you think otherwise, show proof.
>
> --
> Paul Phillips      | We act as though comfort and luxury were the chief
> Apatheist          | requirements of life, when all that we need to make us
> Empiricist         | really happy is something to be enthusiastic about.
> slap pi uphill!    |     -- Charles Kingsley
>



--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.

Re: What to expect from println

by Erlend Hamnaberg-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

To get the correct toString use Arrays.toString(array)

On Oct 31, 2009 3:07 AM, "Daniel Sobral" <dcsobral@...> wrote:

No, this is Java's toString on Arrays. Previously, Scala pretended to have their own Arrays, but that turned out to be impossible to do consistently. As you have verified yourself.

Since it couldn't be done right, now Arrays are Java Arrays.

On Fri, Oct 30, 2009 at 6:52 PM, jlist9 <jlist9@...> wrote: > > Oh in 2.8 println(a.toString...

--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.