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

Confused by https://issues.scala-lang.org/browse/SI-490

4 replies
Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 3 days ago.
Apologies if you think this should be on scala user. 
Paul's recent fix to this issue specifies the behaviour of getClass. For example:
def f1 = (new B: Any).getClass().newInstance() // Anydef f2 = (new B: AnyRef).getClass().newInstance() // AnyRefdef f3 = (new B: A).getClass().newInstance() // Adef f4 = (new B: B).getClass().newInstance() // B

This looks to me like a complete break with the behaviour I would expect. That is, the result of calling a method on some instance "a" depends on the static type of a's reference - completely at odds with how other "instance" methods work.
Am I missing something?
Chris
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Confused by https://issues.scala-lang.org/browse/SI-490

On 6/27/11 1:17 AM, Chris Marshall wrote:
> This looks to me like a complete break with the behaviour I would
> expect. That is, the result of calling a method on some instance "a"
> depends on the static type of a's reference - completely at odds with
> how other "instance" methods work.

The result doesn't depend on it: all those examples return the same value. The type of the result does. What are these other "instance" methods? newInstance is a method on Class.

It's the same in java.

http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#getCl...

"The actual result type is Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass is called."

adriaanm
Joined: 2010-02-08,
User offline. Last seen 31 weeks 4 days ago.
Re: Confused by https://issues.scala-lang.org/browse/SI-490


On Mon, Jun 27, 2011 at 10:17 AM, Chris Marshall <oxbow_lakes@hotmail.com> wrote:
Am I missing something?
yes, the static-dynamic divide:  
statically, `x.getClass().newInstance()` has type `T` if `x` has type `T`
the runtime behaviour is illustrated below:

Welcome to Scala version 2.10.0.r25149-b20110624145919 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24).Type in expressions to have them evaluated.Type :help for more information.
scala>  class Adefined class A
scala>  class B extends Adefined class B
scala> def f3 = (new B: A).getClass().newInstance() f3: A
scala>  (new B: A).getClass()res0: java.lang.Class[_ <: A] = class B
scala> res0.newInstance()res1: A = B@501d5ebc
Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 3 days ago.
RE: Confused by https://issues.scala-lang.org/browse/SI-490
Apologies - I thought that you were indicating the type of object created, as opposed to the type of its reference. 
By "instance" method, I meant "a method which is not a static method" - old terminology I think I was first indoctrinated with when being taught Smalltalk (except a "static" method would have been called a "class" method in Smalltalk).

Chris
> Date: Mon, 27 Jun 2011 01:27:51 -0700
> From: paulp@improving.org
> To: scala-internals@googlegroups.com
> CC: oxbow_lakes@hotmail.com
> Subject: Re: [scala-internals] Confused by https://issues.scala-lang.org/browse/SI-490
>
> On 6/27/11 1:17 AM, Chris Marshall wrote:
> > This looks to me like a complete break with the behaviour I would
> > expect. That is, the result of calling a method on some instance "a"
> > depends on the static type of a's reference - completely at odds with
> > how other "instance" methods work.
>
> The result doesn't depend on it: all those examples return the same value. The type of the result does. What are these other "instance" methods? newInstance is a method on Class.
>
> It's the same in java.
>
> http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#getCl...
>
> "The actual result type is Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass is called."
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Confused by https://issues.scala-lang.org/browse/SI-490

On 6/27/11 1:43 AM, Chris Marshall wrote:
> By "instance" method, I meant "a method which is not a static method" -

Oh, those are called instance methods.

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