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 .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 <[hidden email]> wrote:
|
On 25 mars 2014, at 11:30, Benjamin <[hidden email]> wrote:
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.
|
In reply to this post by Benjamin Van Ryseghem (Pharo)
2014-03-25 10:30 GMT+00:00 Benjamin <[hidden email]>:
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]".
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:
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, reflection is the ability of a computer program to examine (see type introspection) and modify the structure and behavior (specifically the values, meta-data, properties and functions) of the program at runtime." 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 <[hidden email]>:
Panu |
In reply to this post by Sergi Reyner
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) 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:[hidden email]] 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
|
In reply to this post by Sergi Reyner
On 25 mars 2014, at 11:42, Sergi Reyner <[hidden email]> wrote:
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.
|
In reply to this post by Panu Suominen
2014-03-25 10:50 GMT+00:00 Panu Suominen <[hidden email]>:
It´s probably a slightly poor choice of words :) What I mean by operating at a higher level is that, whereas:
is a single message send to an object, this:
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.
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:[hidden email]] On Behalf Of Sergi Reyner 2014-03-25 10:50 GMT+00:00 Panu Suominen <[hidden email]>:
It´s probably a slightly poor choice of words :) What I mean by operating at a higher level is that, whereas:
is a single message send to an object, this:
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.
Maybe it wasn´t the right choice of words. I accept suggestions of alternatives :) Cheers, Sergi
|
In reply to this post by Sergi Reyner
Sergi Reyner wrote:
First required reading.... https://xkcd.com/386/ Second required reading... http://paulgraham.com/avg.html Skip down the section "The Blub Paradox" Now having said that :) even though I'm not an expert in either Java or Smalltalk, I'm happy to have a crack (and learn something in the process where I'm wrong) You might not need to win the debate, but some examples might be useful. First showing how /this/ & /thisContext/ are different while /this/ & /self/ are the same. Object subclass: #ContextExampleA. ContextExampleA subclass: #SubA. ContextExampleA>>whoAreYou ^ 'I am A'. SubA>> whoAreYou ^ 'I am SubA'. ContextExampleA>> report1 Transcript show: self whoAreYou. ContextExampleA>> report2 Transcript show: thisContext whoAreYou. ---------------- So evaluating... MySuper new report1. MySub new report1 outputs... I am A I am SubA and evaluating... MySuper new report2 produces Error - /thisContext/ does not understand message #whoAreYou --------------- Object subclass: #ContextExampleB. Object subclass: #ContextExampleC. ContextExampleB>>whoAreYou ^ 'I am B'. ContextExampleC>>whoAreYou ^ 'I am C'. ContextExampleA >> reportA ContextExampleB new reportB. ContextExampleB >> reportB ContextExampleC new reportC. ContextExampleC >> reportC | context | context := thisContext. 3 timesRepeat: [ Transcript show: context printString ; tab ; show: context receiver whoAreYou; cr. context := context sender. ]. thisContext inspect. ---------- So that evaluating... ContextExampleA new reportA outputs... ContextExampleC>>reportC I am C ContextExampleB>>reportB I am B ContextExampleA>>reportA I am A ----------- Here the execution call stack has been traversed to send a message to a receiver a few levels down. I'm not sure of the practical utility of that, but how would Java do a similar thing? cheers -ben |
Free forum by Nabble | Edit this page |