How can I tell where a message was sent from while in a method? For instance, I have a class called Reminder that has a class method called todo: that receives a string:
Reminder >> todo: aString Transcript show: aString. With the following as an example:
SomeClass >> doSomething Reminder todo: 'Fix this'. When Reminder todo: in doSomething is invoked, how can I tell where todo: was sent from? Thanks Sean |
On 5/17/07, Sean McGinty <[hidden email]> wrote:
> How can I tell where a message was sent from while in a method? thisContext sender printString |
In reply to this post by Sean McGinty
If your Reminder rely on
knowledge about a sender, I would make it clear passing a sender
as an argument:
Reminder >> todo:
aString for: sender
Transcript show:
aString.
"Use sender as you need"
SomeClass >>
doSomething
Reminder todo: 'Fix
this' for: self.
|
In reply to this post by Sean McGinty
If you are doing this for debugging, you might look at this post:
http://www.cincomsmalltalk.com/userblogs/runarj/blogView?showComments=true&printTitle=Logging_Context&entry=3254739381 Runar Jordahl |
In reply to this post by Vassili Bykov-2
Vassili Bykov wrote:
> On 5/17/07, Sean McGinty <[hidden email]> wrote: > > How can I tell where a message was sent from while in a method? > > thisContext sender printString > The VW debugger inserts stack frames when you are stepping through methods, so when debugging this will likely get a different result than when you are not debugging. This probably doesn't matter in this case, but if it does, the other approach would be to put the thisContext reference in the method that you care about and implement your method on MethodContext. For example: MethodContext>>remindToDo: aString Transcript cr; show: aString, ': ', self printString. someMethod thisContext remindTodo: 'Fix this'. David |
In reply to this post by Sean McGinty
Sean McGinty wrote:
> How can I tell where a message was sent from while in a method? For > instance, I have a class called Reminder that has a class method called > todo: that receives a string: > > Reminder >> todo: aString > Transcript show: aString. > > With the following as an example: > > SomeClass >> doSomething > > Reminder todo: 'Fix this'. > > When Reminder todo: in doSomething is invoked, how can I tell where > todo: was sent from? > > Thanks > > Sean > > > you could also implement #todo: in Object like this: Object>>todo: aString Reminder todo: aString sender: self and change your example: SomeClass>>doSomething self todo: 'Fix this' I suggest you stay away from thisContext trickery if there are other ways... Cheers Wolfgang |
> > I suggest you stay away from thisContext > trickery if there are other ways... > Why have #todo: print anything at all? Reminders are meta information so you can use the tools you already have, like 'senders of'. This way there is no need to even ponder an implementation for you reminders :-) R - |
In reply to this post by Sean McGinty
At 06:09 PM 5/17/2007, Sean McGinty wrote:
>How can I tell where a message was sent from while in a method? For instance, I have a class called Reminder that has a class method called todo: that receives a string: > >Reminder >> todo: aString >Transcript show: aString. > >With the following as an example: > >SomeClass >> doSomething > >Reminder todo: 'Fix this'. > >When Reminder todo: in doSomething is invoked, how can I tell where todo: was sent from? Hi Sean, For the answer "as asked": See other posts in this thread (or find it buried in the following). Taking your question at face value, here is a quick rundown of the way this issue is typically addressed. There were hints, but it's worth a further look. One can get an awful lot of mileage out of just these two techniques. (season to taste, but sparingly.) Regards, -cstb =============================================================== The really quick hacks: (lookup senders of #fixMe or #revisit). The secret of success: avoid temptation to add more tag words. Todo has two very distinct barks - so it pays to differentiate. =============================================================== SomeClass>>doSomething #fixMe. ... " some broken code - need to fix it before we ship. " ... SomeClass>>doSomething #revisit. ... " good enough for now, but could be improved - later. " ... =================================================================== The above works just fine, until -- suddenly, you're not in Kansas anymore. You may say to yourself: "This is not my beautiful house. How did I get here?" The basic tagAlog idiom: (lookup senders of #flag: or #flag:with:) =================================================================== SomeClass>>doSomething self flag: 'fix this'. ... Object>>flag: something self whenLogging ifTrue: [self log: something] Object>>flag: someoneOrSomething with: moreInfo self whenLogging ifTrue: [self log: someoneOrSomething with: moreInfo] Object>>whenLogging ^self isLogging or: [self controlKeyIsDown] Object>>isLogging " subclasses override this to turn logging on " ^false Object>>log ^Transcript Object>>log: something self loggerHeads: something. self log cr. Object>>log: something with: moreInfo self loggerHeads: something ; loggerTails: moreInfo Object>>loggerHeads: deheads self log nextPutAll: Timestamp now printstring ; nextPutAll: ' ' ; nextPutAll: thisContext sender printstring ; nextPutAll: '>' ; nextPutAll: deheads printString Object>>loggerTails: details self log nextPutAll: '(' ; nextPutAll: details printString ; nextPutAll: ')' ; cr ============================================ Optional multi-developer/project shortcuts. Tradeoff: more things to lookup senders-of. sean works on 'foo' cstb works on 'bar' but assignments can change, over time, so: ============================================ SomeClass>>doSomething self flagAll: 'fix this'. ... self flagFoo: 'fix that'. ... self flagBar: 'add class comments' ... Object>>flagAll: reminder self flag: #all with: reminder Object>>flagFoo: reminder self flag: #sean with: reminder Object>>flagBar: reminder self flag: #cstb with: reminder Regards, -cstb |
Free forum by Nabble | Edit this page |