Java "this" against our "thisContext"
Hi everyone, I had a pretty heated IRC debate yesterday about how Smalltalk´s reflection facilities are superior to those of Java, in the sense that they operate at a higher level (I started it pasting one of those crazy snippets that got posted to the list xD). It somehow slowly degenerated into someone trying to convince me that Java's "this" operator is equivalent to "thisContext", instead of "self". My intuition says that´s wrong, but I´d very much like someone familiar with both to offer an explanation to this poor unenlightened person :D Cheers, Sergi
Within an instance method or a constructor, this is a reference to the current object â the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this. from: http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html thisContext in smalltalk points to the execution context of the method being currently evaluated. So definitely this and self are the âsameâ (as long as we do not talk about inner classes) thisContext does not exist in Java AFAIK Ben On 25 Mar 2014, at 11:27, Sergi Reyner <sergi.reyner@gmail.com> wrote:
Hi everyone,
I had a pretty heated IRC debate yesterday about how Smalltalk´s reflection facilities are superior to those of Java, in the sense that they operate at a higher level (I started it pasting one of those crazy snippets that got posted to the list xD). It somehow slowly degenerated into someone trying to convince me that Java's "this" operator is equivalent to "thisContext", instead of "self".
My intuition says that´s wrong, but I´d very much like someone familiar with both to offer an explanation to this poor unenlightened person :D
Cheers, Sergi
On 25 mars 2014, at 11:30, Benjamin <benjamin.vanryseghem.pharo@gmail.com> wrote:
Within an instance method or a constructor, this is a reference to the current object â the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
from: http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
thisContext in smalltalk points to the execution context of the method being currently evaluated. So definitely this and self are the âsameâ (as long as we do not talk about inner classes)
thisContext does not exist in Java AFAIK
You can get a reification of the stack with simple introspection (get the method, class or file name and the line number) with Thread.currentThread().getStackTrace() But this is far from the fully reflective Smalltalk stack and the advanced manipulations it enables.
Ben
On 25 Mar 2014, at 11:27, Sergi Reyner <sergi.reyner@gmail.com> wrote:
Hi everyone,
I had a pretty heated IRC debate yesterday about how Smalltalk´s reflection facilities are superior to those of Java, in the sense that they operate at a higher level (I started it pasting one of those crazy snippets that got posted to the list xD). It somehow slowly degenerated into someone trying to convince me that Java's "this" operator is equivalent to "thisContext", instead of "self".
My intuition says that´s wrong, but I´d very much like someone familiar with both to offer an explanation to this poor unenlightened person :D
Cheers, Sergi
2014-03-25 10:30 GMT+00:00 Benjamin <benjamin.vanryseghem.pharo@gmail.com>:
Within an instance method or a constructor, this is a reference to the *current object* -- the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
from: http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
thisContext in smalltalk points to the execution context of the method being currently evaluated. So definitely this and self are the "same" (as long as we do not talk about inner classes)
Yeah, that part I truly understand.I do know enough Java and Smalltalk to understand that "this" and "self" are equivalent (topic which somehow evaporated from the debate, and replaced with "this is the same and thisContext because [unintelligible Java code]".
thisContext does not exist in Java AFAIK
That´s exactly what my intuition led me to. But when I tried to explain that I, too, don´t think that Java has a concept of "thisContext" I was presented with this: Field a = sun.misc.VM.class.getDeclaredField("directMemory"); a.setAccessible(true); a.get(null); Which, after some investigation, and as far as I understood, looks to me as if all it does is retrieve what "this" points to. It´s not really relevant because what I was arguing is that Smalltalk reflection operates at a higher level, which that piece of code proves pretty nicely, but since we got there I´d like to understand everything argued. Cheers, Sergi PS: Note that I´m not trying to win an internet debate, but simply to understand :)
What do you mean operates at higher level?
From Wikipedia: "In computer science <https://en.wikipedia.org/wiki/Computer_science>, *reflection* is the ability of a computer program<https://en.wikipedia.org/wiki/Computer_program> to examine (see type introspection<https://en.wikipedia.org/wiki/Type_introspection>) and modify the structure and behavior (specifically the values, meta-data, properties and functions) of the program at runtime<https://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase)> ."
Most things are much harder to do in Java than in Smalltalk but I don't know if the level differs so much. For example method substitution is not so standard VM feature in Java. 2014-03-25 12:42 GMT+02:00 Sergi Reyner <sergi.reyner@gmail.com>:
2014-03-25 10:30 GMT+00:00 Benjamin <benjamin.vanryseghem.pharo@gmail.com> :
Within an instance method or a constructor, this is a reference to the *current
object* â the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
from: http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
thisContext in smalltalk points to the execution context of the method being currently evaluated. So definitely this and self are the âsameâ (as long as we do not talk about inner classes)
Yeah, that part I truly understand.I do know enough Java and Smalltalk to understand that "this" and "self" are equivalent (topic which somehow evaporated from the debate, and replaced with "this is the same and thisContext because [unintelligible Java code]".
thisContext does not exist in Java AFAIK
That´s exactly what my intuition led me to. But when I tried to explain that I, too, don´t think that Java has a concept of "thisContext" I was presented with this:
Field a = sun.misc.VM.class.getDeclaredField("directMemory"); a.setAccessible(true); a.get(null);
Which, after some investigation, and as far as I understood, looks to me as if all it does is retrieve what "this" points to.
It´s not really relevant because what I was arguing is that Smalltalk reflection operates at a higher level, which that piece of code proves pretty nicely, but since we got there I´d like to understand everything argued.
Cheers, Sergi
PS: Note that I´m not trying to win an internet debate, but simply to understand :)
-- Panu
2014-03-25 10:50 GMT+00:00 Panu Suominen <panu.suominen@iki.fi>:
What do you mean operates at higher level?
It´s probably a slightly poor choice of words :) What I mean by operating at a higher level is that, whereas: thisContext instVarNamed: #receiver put: 42 is a single message send to an object, this: Field a = sun.misc.VM.class.getDeclaredField("directMemory"); a.setAccessible(true); a.get(null); somehow makes me think about accessing internals that I shouldn´t care about. I´m not sure of what the Java code does, as far as I can tell the intention was to show me how you can access 'this' in Java. It doesn´t look very object-oriented to me, but I can´t produce a "formal" explanation of why. Most things are much harder to do in Java than in Smalltalk but I don't
know if the level differs so much. For example method substitution is not so standard VM feature in Java.
Maybe it wasn´t the right choice of words. I accept suggestions of alternatives :) Cheers, Sergi
Check the FileSystem-Core-Implementation package and ask them to do the Guides and Visitors in Java. Their number of lines should be a couple of times more than these. Or check the Pharo 3Âs * PharoClassInstaller>>migrateClasses: old to: new using: anInstanceModification * SlotClassBuilder Yeah, sure, doing that in Java doable. But what a deep pain where you can for sure figure out. Phil From: Pharo-users [mailto:pharo-users-bounces@lists.pharo.org] On Behalf Of Sergi Reyner Sent: mardi 25 mars 2014 13:17 To: Any question about pharo is welcome Subject: Re: [Pharo-users] Java "this" against our "thisContext" 2014-03-25 10:50 GMT+00:00 Panu Suominen <panu.suominen@iki.fi <mailto:panu.suominen@iki.fi> >: What do you mean operates at higher level? It´s probably a slightly poor choice of words :) What I mean by operating at a higher level is that, whereas: thisContext instVarNamed: #receiver put: 42 is a single message send to an object, this: Field a = sun.misc.VM.class.getDeclaredField("directMemory"); a.setAccessible(true); a.get(null); somehow makes me think about accessing internals that I shouldn´t care about. I´m not sure of what the Java code does, as far as I can tell the intention was to show me how you can access 'this' in Java. It doesn´t look very object-oriented to me, but I can´t produce a "formal" explanation of why. Most things are much harder to do in Java than in Smalltalk but I don't know if the level differs so much. For example method substitution is not so standard VM feature in Java. Maybe it wasn´t the right choice of words. I accept suggestions of alternatives :) Cheers, Sergi --- Ce courrier électronique ne contient aucun virus ou logiciel malveillant parce que la protection avast! Antivirus est active. http://www.avast.com
On 25 mars 2014, at 11:42, Sergi Reyner <sergi.reyner@gmail.com> wrote:
2014-03-25 10:30 GMT+00:00 Benjamin <benjamin.vanryseghem.pharo@gmail.com>: Within an instance method or a constructor, this is a reference to the current object â the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
from: http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
thisContext in smalltalk points to the execution context of the method being currently evaluated. So definitely this and self are the âsameâ (as long as we do not talk about inner classes)
Yeah, that part I truly understand.I do know enough Java and Smalltalk to understand that "this" and "self" are equivalent (topic which somehow evaporated from the debate, and replaced with "this is the same and thisContext because [unintelligible Java code]".
thisContext does not exist in Java AFAIK
That´s exactly what my intuition led me to. But when I tried to explain that I, too, don´t think that Java has a concept of "thisContext" I was presented with this:
Field a = sun.misc.VM.class.getDeclaredField("directMemory"); a.setAccessible(true); a.get(null);
Which, after some investigation, and as far as I understood, looks to me as if all it does is retrieve what "this" points to.
For me, that snippet just returns the value of a static field named directMemory in the class sun.misc.VM. Field a = sun.misc.VM.class.getDeclaredField("directMemory"); // reify the field a.setAccessible(true); // by-pass visibility checks (maybe it's private) a.get(null); // get the value of that field So that's just an example of simple introspection, but it's not related to stack introspection.
It´s not really relevant because what I was arguing is that Smalltalk reflection operates at a higher level, which that piece of code proves pretty nicely, but since we got there I´d like to understand everything argued.
Cheers, Sergi
PS: Note that I´m not trying to win an internet debate, but simply to understand :)
It's not a direct answer, but an interesting side-light: The recent announcement of Gravel Smalltalk, billed as Smalltalk for the Java VM ( <https://github.com/gravel-st/gravel> https://github.com/gravel-st/gravel) mentions as 'common Smalltalk features that we probably won't support' both become: and thisContext. Presumably omitted because they can't be done in Java. Peter Kenny _____ From: Pharo-users [mailto:pharo-users-bounces@lists.pharo.org] On Behalf Of Sergi Reyner Sent: 25 March 2014 10:27 To: Any question about pharo is welcome Subject: [Pharo-users] Java "this" against our "thisContext" Hi everyone, I had a pretty heated IRC debate yesterday about how Smalltalk´s reflection facilities are superior to those of Java, in the sense that they operate at a higher level (I started it pasting one of those crazy snippets that got posted to the list xD). It somehow slowly degenerated into someone trying to convince me that Java's "this" operator is equivalent to "thisContext", instead of "self". My intuition says that´s wrong, but I´d very much like someone familiar with both to offer an explanation to this poor unenlightened person :D Cheers, Sergi
participants (7)
-
Ben Coman -
Benjamin -
Camille Teruel -
Panu Suominen -
PBK Research -
Philippe Back -
Sergi Reyner