[comp.lang.smalltalk] Show a method name

schultz@grebyn.com (Ronald Schultz) (07/13/90)

Hopefully this is a simple question.

I have a method, and all I want to do is have the method return it's
name.  For a class I would simply say

^self class name.

How do I do this ?

Thanx.

schultz@grebyn.com

moss@cs.umass.edu (Eliot Moss) (07/13/90)

It's not trivial to find a method's name from within the method, but since you
(of course) *know* the name you can simply return it, i.e., if the method is
foo, you simply write:

	^ #foo

Now if you want to try to find out the method's name otherwise, you have to go
through the receiver's class, superclass, super-superclass, etc., scan each
class's method table for the method in question, and then grab the name from
the parallel part of the method table. Even this could, I suppose, fail under
strange circumstances (e.g., the method has been since recompiled, and the
version you are running is reachable only from contexts (stack frames) that
involed it before the recompilation). There may be existing Smalltalk code for
discovering the name of the method; in fact, there must be, since the system
prints out nice stack backtraces. I'd start with the implementation of
Object|halt (the breakpoint method) and track down the stack backtrace code.
Best of luck!							Eliot
--

		J. Eliot B. Moss, Assistant Professor
		Department of Computer and Information Science
		Lederle Graduate Research Center
		University of Massachusetts
		Amherst, MA  01003
		(413) 545-4206; Moss@cs.umass.edu

phil@abccam.abcl.co.uk (Phillip Yelland) (07/16/90)

Try the following (esp. in ParcPlace V2.3):

-------------------------------------------------------------------------

<method name of your choice>

	"Quick hack to return the name of this method---whatever it is"

	^self class
		selectorAtMethod: thisContext method
		setClass: [:ignored | ]

-------------------------------------------------------------------------

Hope this helps.

Regards,

--Phil

sdl@linus.mitre.org (Steven D. Litvinchouk) (07/17/90)

In article <20310@grebyn.com> schultz@grebyn.com (Ronald Schultz) writes:

> I have a method, and all I want to do is have the method return it's
> name....
> How do I do this ?

In ParcPlace Smalltalk-80, inside a given method, the expression
	thisContext selector
will return the selector of the currently executing method, and 
	thisContext sourceCode
will return its source code.

It works because 'thisContext' is a context variable that holds the
currently executing method or block context.

Perhaps some equivalent thing can be done in Smalltalk/V ?

--

Steven Litvintchouk
MITRE Corporation
Burlington Road
Bedford, MA  01730
(617)271-7753

ARPA:  sdl@mbunix.mitre.org
UUCP:  ...{att,decvax,genrad,ll-xn,philabs,utzoo}!linus!sdl

	"Where does he get those wonderful toys?"
				-- J. Napier (a.k.a. "The Joker")

obryan@gumby.cc.wmich.edu (Mark O'Bryan) (07/17/90)

In article <20310@grebyn.com> schultz@grebyn.com (Ronald Schultz) writes:
> Hopefully this is a simple question.
> 
> I have a method, and all I want to do is have the method return it's
> name.  For a class I would simply say
> 
> ^self class name.
> 
> How do I do this ?

You might want to try:

   ^self selector printString

-- 
Mark T. O'Bryan                 Internet:  obryan@gumby.cc.wmich.edu
Western Michigan University
Kalamazoo, MI  49008

tma@osc.COM (Tim Atkins) (07/17/90)

In article <20310@grebyn.com> schultz@grebyn.com (Ronald Schultz) writes:
>Hopefully this is a simple question.
>
>I have a method, and all I want to do is have the method return it's
>name.  For a class I would simply say
>
>^self class name.
>
>How do I do this ?
>
>Thanx.
>
>schultz@grebyn.com


I don't have a Smalltalk-80 handy but the symbol for the method
selector can be found in Smalltalk V by

  ^(self class selectorFor:(Process copyStack methodAt:0)) key

Although this is obviously woefully inefficient, I hope it provides a clue.

- Tim