Runtime instantiation of an unknown class/method 
Author Message
 Runtime instantiation of an unknown class/method

From a begginer:

I have a design that requires Smalltalk to start a smalltalk thread
specified by a message received via a socket.  I already have it
starting external programs using WinExec (via KernelLibrary)

Now, since the message will be text, all I have are strings to deal
with, right?  I'm assuming I may be able to look-up the class in
Smalltalk's dictionary, but I'm unsure how I'm going to send a string
message to it, since messages are symbols (aren't they?).

I'm using Dolphin but didn't think that should play a significant role
in determining who should read this post, so I didn't put it in the
subject line.

Would something like this work?  Would it be dangerous?

invoke: aClassString with: aMethodString
        (Smalltalk at: aClassString) perform: aMethodString



Wed, 18 Jun 1902 08:00:00 GMT  
 Runtime instantiation of an unknown class/method



Quote:
>From a begginer:

>I have a design that requires Smalltalk to start a smalltalk thread
>specified by a message received via a socket.  I already have it
>starting external programs using WinExec (via KernelLibrary)

>Now, since the message will be text, all I have are strings to deal
>with, right?  I'm assuming I may be able to look-up the class in
>Smalltalk's dictionary, but I'm unsure how I'm going to send a string
>message to it, since messages are symbols (aren't they?).

>I'm using Dolphin but didn't think that should play a significant role
>in determining who should read this post, so I didn't put it in the
>subject line.

>Would something like this work?  Would it be dangerous?

>invoke: aClassString with: aMethodString
>    (Smalltalk at: aClassString) perform: aMethodString

Very close.

In 'Smalltalk '  (the object, not the language).  The keys are held as
Symbols, not Strings  Also, the parameter in the message send
#perform: is also expected to be a Symbol not a String.  However,
Symbols and Strings are closely related and you can easily convert a
String to a Symbol with the message #asSymbol.  So your above method
should be written.

invoke: aClassString with: aMethodString

        (Smalltalk at: aClassString asSymbol)
                perform: aMethodString asSymbol.

or

invoke: aClassString with: aMethodString

        | targetClass |

        targetClass := Smalltalk at: aClassString asSymbol
ifAbsent:[nil].

        targetClass isNil ifFalse: [targetClass perform: aMethodString
asSymbol]

There are syntacticacl ways of shortening that but...

Take care,
Jay O'Connor

http://www.jmpstart.com

"You can fight without winning, but never win without a fight
You can surrender without a prayer, but never pray without surrender"



Wed, 18 Jun 1902 08:00:00 GMT  
 Runtime instantiation of an unknown class/method


Quote:

>In 'Smalltalk '  (the object, not the language).  The keys are held as
>Symbols, not Strings  Also, the parameter in the message send
>#perform: is also expected to be a Symbol not a String.  However,
>Symbols and Strings are closely related and you can easily convert a
>String to a Symbol with the message #asSymbol.  So your above method
>should be written.

>invoke: aClassString with: aMethodString

>    (Smalltalk at: aClassString asSymbol)
>            perform: aMethodString asSymbol.

>or

>invoke: aClassString with: aMethodString

>    | targetClass |

>    targetClass := Smalltalk at: aClassString asSymbol
>ifAbsent:[nil].

>    targetClass isNil ifFalse: [targetClass perform: aMethodString
>asSymbol]

>There are syntacticacl ways of shortening that but...

There are things in the system dictionary 'Smalltalk' that are not
classes. You should check to see if targetClass is really a class. Some
systems have an #isClass method which makes it easy. Just change that
last line:

   targetClass isClass ifTrue: [
      targetClass perform: aMethodString asSymbol]

For those that don't, either add two #isClass methods, one in Object
which answers false and one in class Class which answers true, or do
something like:

 (Smalltalk at: #Bag) class class

which always answers the class Metaclass if the object is a class and
always works but never answers Metaclass if it isn't:

   (Smalltalk at: #CldtConstants) class class
   EsPoolDictionary class

   nil class class
   UndefinedObject class

Thus, the test is:

   targetClass class class == Metaclass ifTrue: [ ... ]

Note that some implementations spell Metaclass 'MetaClass'.

Dave

_____________________________________________
David N. Smith
IBM T J Watson Research Center, Hawthorne, NY

Home Page: http://www.dnsmith.com/
_____________________________________________
Any opinions or recommendations are those
of the author and not of his employer.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Runtime object type determination/ runtime generic instantiation

2. Add methods to a class at runtime?

3. adding instance methods after instantiation

4. accessing a instance method from a class method

5. How do I call a method from another method within the same class

6. Class methods vs Instance methods

7. Class method vs instance method???

8. Class method vs instance method???

9. Class methods in ab???.clw files and derived methods

10. Instantiation Control of generic classes

11. Instantiation Control of generic classes

12. same method as class and instance methods?

 

 
Powered by phpBB® Forum Software