Quantcast

Structural types/reflection

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Structural types/reflection

Adam Rabung
Hi,
I've been wondering for quite some time if there's hope that this form

val x = new { def foo = 5 }
x.foo

Could some day be optimized to not use reflection?  

Thank you,
Adam
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Structural types/reflection

Kevin Wright-3
val x = new Object { def foo = 5 } // <-- anonymous but non-structural class



On 3 December 2010 19:08, Adam Rabung <[hidden email]> wrote:
Hi,
I've been wondering for quite some time if there's hope that this form

val x = new { def foo = 5 }
x.foo

Could some day be optimized to not use reflection?  

Thank you,
Adam



--
Kevin Wright

mail / gtalk / msn : [hidden email]
pulse / skype: kev.lee.wright
twitter: @thecoda

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Structural types/reflection

Adam Rabung
That seems to use reflection.

On Fri, Dec 3, 2010 at 2:14 PM, Kevin Wright <[hidden email]> wrote:
val x = new Object { def foo = 5 } // <-- anonymous but non-structural class



On 3 December 2010 19:08, Adam Rabung <[hidden email]> wrote:
Hi,
I've been wondering for quite some time if there's hope that this form

val x = new { def foo = 5 }
x.foo

Could some day be optimized to not use reflection?  

Thank you,
Adam



--
Kevin Wright

mail / gtalk / msn : [hidden email]
pulse / skype: kev.lee.wright
twitter: @thecoda


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Structural types/reflection

Kevin Wright-3
hmm, how do you determine that?



On 3 December 2010 19:22, Adam Rabung <[hidden email]> wrote:
That seems to use reflection.


On Fri, Dec 3, 2010 at 2:14 PM, Kevin Wright <[hidden email]> wrote:
val x = new Object { def foo = 5 } // <-- anonymous but non-structural class



On 3 December 2010 19:08, Adam Rabung <[hidden email]> wrote:
Hi,
I've been wondering for quite some time if there's hope that this form

val x = new { def foo = 5 }
x.foo

Could some day be optimized to not use reflection?  

Thank you,
Adam



--
Kevin Wright

mail / gtalk / msn : [hidden email]
pulse / skype: kev.lee.wright
twitter: @thecoda





--
Kevin Wright

mail / gtalk / msn : [hidden email]
pulse / skype: kev.lee.wright
twitter: @thecoda

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Structural types/reflection

Sc iss
scala> new Object { def foo = 5 }
res22: java.lang.Object{def foo: Int} = $anon$1@14b9e0c

scala> class A extends Object { def foo = 5 }
defined class A

scala> new A
res23: A = A@1893c7

the string representation seems to be at least an indication that this is happening...


Am 03.12.2010 um 19:33 schrieb Kevin Wright:

> hmm, how do you determine that?
>
>
>
> On 3 December 2010 19:22, Adam Rabung <[hidden email]> wrote:
> That seems to use reflection.
>
>
> On Fri, Dec 3, 2010 at 2:14 PM, Kevin Wright <[hidden email]> wrote:
> val x = new Object { def foo = 5 } // <-- anonymous but non-structural class
>
>
>
> On 3 December 2010 19:08, Adam Rabung <[hidden email]> wrote:
> Hi,
> I've been wondering for quite some time if there's hope that this form
>
> val x = new { def foo = 5 }
> x.foo
>
> Could some day be optimized to not use reflection?  
>
> Thank you,
> Adam
>
>
>
> --
> Kevin Wright
>
> mail / gtalk / msn : [hidden email]
> pulse / skype: kev.lee.wright
> twitter: @thecoda
>
>
>
>
>
> --
> Kevin Wright
>
> mail / gtalk / msn : [hidden email]
> pulse / skype: kev.lee.wright
> twitter: @thecoda
>

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Structural types/reflection

Adam Rabung
def parentMethod() {
        val x = new Object {
                def foo =  {
                        println(new Exception().getStackTrace().take(6).mkString("\n\t"));
                        5
                }
        }
        x.foo
}
parentMethod()

prints out
Main$$anon$1$$anon$2.foo(ss.scala:4)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        java.lang.reflect.Method.invoke(Method.java:597)
        Main$$anon$1.parentMethod(ss.scala:8)

On Fri, Dec 3, 2010 at 2:38 PM, Sciss <[hidden email]> wrote:
scala> new Object { def foo = 5 }
res22: java.lang.Object{def foo: Int} = $anon$1@14b9e0c

scala> class A extends Object { def foo = 5 }
defined class A

scala> new A
res23: A = A@1893c7

the string representation seems to be at least an indication that this is happening...


Am 03.12.2010 um 19:33 schrieb Kevin Wright:

> hmm, how do you determine that?
>
>
>
> On 3 December 2010 19:22, Adam Rabung <[hidden email]> wrote:
> That seems to use reflection.
>
>
> On Fri, Dec 3, 2010 at 2:14 PM, Kevin Wright <[hidden email]> wrote:
> val x = new Object { def foo = 5 } // <-- anonymous but non-structural class
>
>
>
> On 3 December 2010 19:08, Adam Rabung <[hidden email]> wrote:
> Hi,
> I've been wondering for quite some time if there's hope that this form
>
> val x = new { def foo = 5 }
> x.foo
>
> Could some day be optimized to not use reflection?
>
> Thank you,
> Adam
>
>
>
> --
> Kevin Wright
>
> mail / gtalk / msn : [hidden email]
> pulse / skype: kev.lee.wright
> twitter: @thecoda
>
>
>
>
>
> --
> Kevin Wright
>
> mail / gtalk / msn : [hidden email]
> pulse / skype: kev.lee.wright
> twitter: @thecoda
>


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Structural types/reflection

Kevin Wright-3
Well I'll be damned...
That syntax even works in Java, so obviously structural typing isn't needed to make this happen.


On 3 December 2010 19:40, Adam Rabung <[hidden email]> wrote:
def parentMethod() {
        val x = new Object {
                def foo =  {
                        println(new Exception().getStackTrace().take(6).mkString("\n\t"));
                        5
                }
        }
        x.foo
}
parentMethod()

prints out
Main$$anon$1$$anon$2.foo(ss.scala:4)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        java.lang.reflect.Method.invoke(Method.java:597)
        Main$$anon$1.parentMethod(ss.scala:8)

On Fri, Dec 3, 2010 at 2:38 PM, Sciss <[hidden email]> wrote:
scala> new Object { def foo = 5 }
res22: java.lang.Object{def foo: Int} = $anon$1@14b9e0c

scala> class A extends Object { def foo = 5 }
defined class A

scala> new A
res23: A = A@1893c7

the string representation seems to be at least an indication that this is happening...


Am 03.12.2010 um 19:33 schrieb Kevin Wright:

> hmm, how do you determine that?
>
>
>
> On 3 December 2010 19:22, Adam Rabung <[hidden email]> wrote:
> That seems to use reflection.
>
>
> On Fri, Dec 3, 2010 at 2:14 PM, Kevin Wright <[hidden email]> wrote:
> val x = new Object { def foo = 5 } // <-- anonymous but non-structural class
>
>
>
> On 3 December 2010 19:08, Adam Rabung <[hidden email]> wrote:
> Hi,
> I've been wondering for quite some time if there's hope that this form
>
> val x = new { def foo = 5 }
> x.foo
>
> Could some day be optimized to not use reflection?
>
> Thank you,
> Adam
>
>
>
> --
> Kevin Wright
>
> mail / gtalk / msn : [hidden email]
> pulse / skype: kev.lee.wright
> twitter: @thecoda
>
>
>
>
>
> --
> Kevin Wright
>
> mail / gtalk / msn : [hidden email]
> pulse / skype: kev.lee.wright
> twitter: @thecoda
>





--
Kevin Wright

mail / gtalk / msn : [hidden email]
pulse / skype: kev.lee.wright
twitter: @thecoda

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Structural types/reflection

HamsterofDeath
really? in java, x would be declared as an object, and you wouldn't be able to call "foo" on it, as far as i remember

Am 03.12.2010 21:00, schrieb Kevin Wright:
Well I'll be damned...
That syntax even works in Java, so obviously structural typing isn't needed to make this happen.


On 3 December 2010 19:40, Adam Rabung <[hidden email]> wrote:
def parentMethod() {
        val x = new Object {
                def foo =  {
                        println(new Exception().getStackTrace().take(6).mkString("\n\t"));
                        5
                }
        }
        x.foo
}
parentMethod()

prints out
Main$$anon$1$$anon$2.foo(ss.scala:4)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        java.lang.reflect.Method.invoke(Method.java:597)
        Main$$anon$1.parentMethod(ss.scala:8)

On Fri, Dec 3, 2010 at 2:38 PM, Sciss <[hidden email]> wrote:
scala> new Object { def foo = 5 }
res22: java.lang.Object{def foo: Int} = $anon$1@14b9e0c

scala> class A extends Object { def foo = 5 }
defined class A

scala> new A
res23: A = A@1893c7

the string representation seems to be at least an indication that this is happening...


Am 03.12.2010 um 19:33 schrieb Kevin Wright:

> hmm, how do you determine that?
>
>
>
> On 3 December 2010 19:22, Adam Rabung <[hidden email]> wrote:
> That seems to use reflection.
>
>
> On Fri, Dec 3, 2010 at 2:14 PM, Kevin Wright <[hidden email]> wrote:
> val x = new Object { def foo = 5 } // <-- anonymous but non-structural class
>
>
>
> On 3 December 2010 19:08, Adam Rabung <[hidden email]> wrote:
> Hi,
> I've been wondering for quite some time if there's hope that this form
>
> val x = new { def foo = 5 }
> x.foo
>
> Could some day be optimized to not use reflection?
>
> Thank you,
> Adam
>
>
>
> --
> Kevin Wright
>
> mail / gtalk / msn : [hidden email]
> pulse / skype: kev.lee.wright
> twitter: @thecoda
>
>
>
>
>
> --
> Kevin Wright
>
> mail / gtalk / msn : [hidden email]
> pulse / skype: kev.lee.wright
> twitter: @thecoda
>





--
Kevin Wright

mail / gtalk / msn : [hidden email]
pulse / skype: kev.lee.wright
twitter: @thecoda


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Structural types/reflection

Stephen Tu
In reply to this post by Kevin Wright-3
very interesting

$ cat Blah.scala
object Blah {
def foo() {
  val x = new Object {
    def bar = {
      new Exception().printStackTrace()
      5
    }
  }
  x.bar
}
}
$ scalac Blah.scala
$ javap -verbose 'Blah$'
[...]
public void foo();
  Code:
   Stack=3, Locals=5, Args_size=1
   0:    new    #58; //class Blah$$anon$1
   3:    dup
   4:    invokespecial    #59; //Method Blah$$anon$1."<init>":()V
   7:    astore_1
   8:    aload_1
   9:    astore_2
   10:    aconst_null
   11:    astore_3
   12:    aload_2
   13:    invokevirtual    #65; //Method java/lang/Object.getClass:()Ljava/lang/Class;
   16:    invokestatic    #67; //Method reflMethod$Method1:(Ljava/lang/Class;)Ljava/lang/reflect/Method;
   19:    aload_2
   20:    iconst_0
   21:    anewarray    #61; //class java/lang/Object
   24:    invokevirtual    #73; //Method java/lang/reflect/Method.invoke:(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;
   27:    astore_3
   28:    aload_3
   29:    checkcast    #75; //class java/lang/Integer
   32:    pop
   33:    return
   34:    astore    4
   36:    aload    4
   38:    invokevirtual    #81; //Method java/lang/reflect/InvocationTargetException.getCause:()Ljava/lang/Throwable;
   41:    athrow
[...]
$ javap 'Blah$$anon$1'
Compiled from "Blah.scala"
public final class Blah$$anon$1 extends java.lang.Object{
    public int bar();
    public Blah$$anon$1();
}

why does foo(), which creates a new instance of Blah$$anon$1, need to use reflection to call bar()? cleary bar() exists in Blah$$anon$1, so is there any reason it couldn't just do an invokevirtual on bar? i'm sure theres something i'm missing here...

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Structural types/reflection

Sc iss
In reply to this post by HamsterofDeath
seems scala is smarter here by considering public methods so that indeed you can call them. compare:

def parentMethod() {
   val x = new Runnable {
      def foo  { // wooop, this elevates the anonymous class to a structural type
         println(new Exception().getStackTrace().take(6).mkString("\n\t"));
      }

      def run = foo
   }
   x.run
}


and

def parentMethod() {
   val x = new Runnable {
      private def foo  {
         println(new Exception().getStackTrace().take(6).mkString("\n\t"));
      }

      def run = foo
   }
   x.run
}

best, -sciss-



Am 03.12.2010 um 20:24 schrieb HamsterofDeath:

> really? in java, x would be declared as an object, and you wouldn't be able to call "foo" on it, as far as i remember
>
> Am 03.12.2010 21:00, schrieb Kevin Wright:
>> Well I'll be damned...
>> That syntax even works in Java, so obviously structural typing isn't needed to make this happen.
>>
>>
>> On 3 December 2010 19:40, Adam Rabung <[hidden email]> wrote:
>> def parentMethod() {
>>         val x = new Object {
>>                 def foo =  {
>>                         println(new Exception().getStackTrace().take(6).mkString("\n\t"));
>>                         5
>>                 }
>>         }
>>         x.foo
>> }
>> parentMethod()
>>
>> prints out
>> Main$$anon$1$$anon$2.foo(ss.scala:4)
>>         sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>         sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>>         sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>>         java.lang.reflect.Method.invoke(Method.java:597)
>>         Main$$anon$1.parentMethod(ss.scala:8)
>>
>> On Fri, Dec 3, 2010 at 2:38 PM, Sciss <[hidden email]> wrote:
>> scala> new Object { def foo = 5 }
>> res22: java.lang.Object{def foo: Int} = $anon$1@14b9e0c
>>
>> scala> class A extends Object { def foo = 5 }
>> defined class A
>>
>> scala> new A
>> res23: A = A@1893c7
>>
>> the string representation seems to be at least an indication that this is happening...
>>
>>
>> Am 03.12.2010 um 19:33 schrieb Kevin Wright:
>>
>> > hmm, how do you determine that?
>> >
>> >
>> >
>> > On 3 December 2010 19:22, Adam Rabung <[hidden email]> wrote:
>> > That seems to use reflection.
>> >
>> >
>> > On Fri, Dec 3, 2010 at 2:14 PM, Kevin Wright <[hidden email]> wrote:
>> > val x = new Object { def foo = 5 } // <-- anonymous but non-structural class
>> >
>> >
>> >
>> > On 3 December 2010 19:08, Adam Rabung <[hidden email]> wrote:
>> > Hi,
>> > I've been wondering for quite some time if there's hope that this form
>> >
>> > val x = new { def foo = 5 }
>> > x.foo
>> >
>> > Could some day be optimized to not use reflection?
>> >
>> > Thank you,
>> > Adam
>> >
>> >
>> >
>> > --
>> > Kevin Wright
>> >
>> > mail / gtalk / msn : [hidden email]
>> > pulse / skype: kev.lee.wright
>> > twitter: @thecoda
>> >
>> >
>> >
>> >
>> >
>> > --
>> > Kevin Wright
>> >
>> > mail / gtalk / msn : [hidden email]
>> > pulse / skype: kev.lee.wright
>> > twitter: @thecoda
>> >
>>
>>
>>
>>
>>
>> --
>> Kevin Wright
>>
>> mail / gtalk / msn : [hidden email]
>> pulse / skype: kev.lee.wright
>> twitter: @thecoda
>>
>

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Structural types/reflection

Paul Phillips-3
In reply to this post by Stephen Tu
On Fri, Dec 03, 2010 at 12:27:49PM -0800, Stephen Tu wrote:
> why does foo(), which creates a new instance of Blah$$anon$1, need to
> use reflection to call bar()? cleary bar() exists in Blah$$anon$1, so
> is there any reason it couldn't just do an invokevirtual on bar? i'm
> sure theres something i'm missing here...

This came up recently in developer discussion: the first ticket led me
to search for all uses of reflection in trunk, and after I listed 100+
in the parallel collections, mgarcia did some speculatin' about how we
might better deal.

  https://lampsvn.epfl.ch/trac/scala/ticket/3974
  https://lampsvn.epfl.ch/trac/scala/ticket/3975

--
Paul Phillips      | These are the climbs that apply men's soles.
Future Perfect     |
Empiricist         |
i pull his palp!   |----------* http://www.improving.org/paulp/ *----------
Loading...