How to access a Squeak class

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

How to access a Squeak class

Warakorn Paphrawat
Hi !
Actually it is probably a very simple Squeak concept which I haven't understood, yet.

Serving as an simple example, I would like to write a method, which accepts a Class as argument and which return the Superclass of that Class.
The problem is that the argument must be an instance of String, that means  the argument aClassName  contains an actual class name (e.g. aClassName = 'Object'. or aClassName = 'ClassBuilder'), however, this is just a String object.

returnSuperClass: aClassName
|aSuperClass|

aSuperClass := aClassName superclass.

^ aSuperClass.

Obviously this method won't work, because aClassName is just an instance of ByteString. And instances of ByteString cannot understand the message superclass.

So I need the actual class object  which the content of the String aClassName represents.
How can I achieve ? Or how can I make this method correct ?
(it is imperative, that the argument aClassName is a String).

Thanks for you help in advance.


Best Regards
Warakorn


Have a burning question? Go to Yahoo! Answers and get answers from real people who know.
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to access a Squeak class

Herbert König
Hello Warakorn,

try:
Smalltalk at: #MonthMorph inspect

Cheers

Herbert       mailto:[hidden email]
WP> Hi !
WP> Actually it is probably a very simple Squeak concept which I haven't understood, yet.

WP> Serving as an simple example, I would like to write a method,
WP> which accepts a Class as argument and which return the Superclass
WP> of that Class.
WP> The problem is that the argument must be an instance of
WP> String, that means  the argument aClassName  contains an actual
WP> class name (e.g. aClassName = 'Object'. or aClassName =
WP> 'ClassBuilder'), however, this is just a String object.

WP> returnSuperClass: aClassName
WP> |aSuperClass|

WP> aSuperClass := aClassName superclass.

WP> ^ aSuperClass.

WP> Obviously this method won't work, because aClassName is just
WP> an instance of ByteString. And instances of ByteString cannot
WP> understand the message superclass.

WP> So I need the actual class object  which the content of the String aClassName represents.
WP> How can I achieve ? Or how can I make this method correct ?
WP> (it is imperative, that the argument aClassName is a String).

WP> Thanks for you help in advance.


WP> Best Regards
WP> Warakorn



WP> Have a burning question? Go to Yahoo! Answers and get answers from real people who know.




_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re[2]: How to access a Squeak class

Herbert König
Hello Warakorn,

HK> try:
HK> Smalltalk at: #MonthMorph inspect

I got some more time, so here is a longer Version:

Smalltalk is a Dictionary which afaik contains the names of all global
symbols. So if you got a String, send it asSymbol and then look it up
in that Dictionary.

Actually Smalltalk is a SystemDictionary, if you are interested in
exploring. The class comment has an example.

Before writing or removing entries in this Dictionary, make sure you
know what you're doing :-))


So your code should be something like:

returnSuperClass: aClassName
|aSuperClass aClass|

aClass :=  Smalltalk at: aClassName asSymbol.
aSuperClass := aClass superclass.

^ aSuperClass.

Cheers

Herbert


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to access a Squeak class

Bert Freudenberg
A better alternative would be

        Smalltalk classNamed: 'Object'

avoiding the "low level" #at: and #asSymbol protocol. Does some more  
checks, too.

- Bert -

On Jan 16, 2007, at 14:33 , Herbert König wrote:

> Hello Warakorn,
>
> HK> try:
> HK> Smalltalk at: #MonthMorph inspect
>
> I got some more time, so here is a longer Version:
>
> Smalltalk is a Dictionary which afaik contains the names of all global
> symbols. So if you got a String, send it asSymbol and then look it up
> in that Dictionary.
>
> Actually Smalltalk is a SystemDictionary, if you are interested in
> exploring. The class comment has an example.
>
> Before writing or removing entries in this Dictionary, make sure you
> know what you're doing :-))
>
>
> So your code should be something like:
>
> returnSuperClass: aClassName
> |aSuperClass aClass|
>
> aClass :=  Smalltalk at: aClassName asSymbol.
> aSuperClass := aClass superclass.
>
> ^ aSuperClass.
>
> Cheers
>
> Herbert
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners




_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to access a Squeak class

Warakorn Paphrawat
In reply to this post by Herbert König
This doesn't work either, because again (Smalltalk at: aClassName) is returning just a ByteString but not (e.g.)     Object class    or   (e.g.) ClassBuilder class .

Lets assume in the calling method is written:
returnSuperClass: '#Object'.
Note: the Argument must be an instance of String.

returnSuperClass: aClassName
|aSuperClass|

aSuperClass := (Smalltalk at: aClassName) superclass.

^ aSuperClass.

Nope, this does'nt work.

Herbert König <[hidden email]> wrote:
Hello Warakorn,

try:
Smalltalk at: #MonthMorph inspect

Cheers

Herbert mailto:[hidden email]
WP> Hi !
WP> Actually it is probably a very simple Squeak concept which I haven't understood, yet.

WP> Serving as an simple example, I would like to write a method,
WP> which accepts a Class as argument and which return the Superclass
WP> of that Class.
WP> The problem is that the argument must be an instance of
WP> String, that means  the argument aClassName  contains an actual
WP> class name (e.g. aClassName = 'Object'. or aClassName =
WP> 'ClassBuilder'), however, this is just a String object.

WP> returnSuperClass: aClassName
WP> |aSuperClass|

WP> aSuperClass := aClassName superclass.

WP> ^ aSuperClass.

WP> Obviously this method won't work, because aClassName is just
WP> an instance of ByteString. And instances of ByteString cannot
WP> understand the message superclass.

WP> So I need the actual class object  which the content of the String aClassName represents.
WP> How can I achieve ? Or how can I make this method correct ?
WP> (it is imperative, that the argument aClassName is a String).

WP> Thanks for you help in advance.


WP> Best Regards
WP> Warakorn



WP> Have a burning question? Go to Yahoo! Answers and get answers from real people who know.




_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


Don't get soaked. Take a quick peak at the forecast
with theYahoo! Search weather shortcut.
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to access a Squeak class

Bert Freudenberg
What makes you think it does not return a class object? It does.

But note that you have to use a symbol (#Object) not a String  
('Object') when you use #at:. Of course. '#Object' does not make any  
sense. If you use #classNamed:, both a String and a Symbol will work.

- Bert -

On Jan 16, 2007, at 14:41 , Warakorn Paphrawat wrote:

> This doesn't work either, because again (Smalltalk at: aClassName)  
> is returning just a ByteString but not (e.g.)     Object class    
> or   (e.g.) ClassBuilder class .
>
> Lets assume in the calling method is written:
> returnSuperClass: '#Object'.
> Note: the Argument must be an instance of String.
>
> returnSuperClass: aClassName
> |aSuperClass|
>
> aSuperClass := (Smalltalk at: aClassName) superclass.
>
> ^ aSuperClass.
>
> Nope, this does'nt work.
>
> Herbert König <[hidden email]> wrote: Hello Warakorn,
>
> try:
> Smalltalk at: #MonthMorph inspect
>
> Cheers
>
> Herbert mailto:[hidden email]
> WP> Hi !
> WP> Actually it is probably a very simple Squeak concept which I  
> haven't understood, yet.
>
> WP> Serving as an simple example, I would like to write a method,
> WP> which accepts a Class as argument and which return the Superclass
> WP> of that Class.
> WP> The problem is that the argument must be an instance of
> WP> String, that means  the argument aClassName  contains an actual
> WP> class name (e.g. aClassName = 'Object'. or aClassName =
> WP> 'ClassBuilder'), however, this is just a String object.
>
> WP> returnSuperClass: aClassName
> WP> |aSuperClass|
>
> WP> aSuperClass := aClassName superclass.
>
> WP> ^ aSuperClass.
>
> WP> Obviously this method won't work, because aClassName is just
> WP> an instance of ByteString. And instances of ByteString cannot
> WP> understand the message superclass.
>
> WP> So I need the actual class object  which the content of the  
> String aClassName represents.
> WP> How can I achieve ? Or how can I make this method correct ?
> WP> (it is imperative, that the argument aClassName is a String).
>
> WP> Thanks for you help in advance.
>
>
> WP> Best Regards
> WP> Warakorn
>
>
>
> WP> Have a burning question? Go to Yahoo! Answers and get answers  
> from real people who know.
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
>
> Don't get soaked. Take a quick peak at the forecast
> with theYahoo! Search weather shortcut.
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re[2]: How to access a Squeak class

Herbert König
In reply to this post by Warakorn Paphrawat
Hello Warakorn,

WP> This doesn't work either, because again (Smalltalk at:
WP> aClassName) is returning just a ByteString but not (e.g.)    
WP> Object class    or   (e.g.) ClassBuilder class .

Copied from a Workspace:

(Smalltalk at: 'MonthMorph' asSymbol) superclass inspect

This returns an inspector with AlignmentMorph class.

I guess you omitted asSymbol and my string doesn't contain the hash
sign.

Please also regard Bert's post he's the professional, I'm still the
beginner.

Cheers

Herbert                            mailto:[hidden email]

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to access a Squeak class

David Mitchell-10
In reply to this post by Warakorn Paphrawat
The method Object>>#class does what you want. Since it is defined on
Object you can send the message to any object, including a string.

> returnSuperClass: aClassName
> |aSuperClass|
>
> aSuperClass := aClassName class superclass.
>
> ^ aSuperClass.
>

The above method body could be a one liner:
^aClassName class superclass
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to access a Squeak class

David Mitchell-10
Doh! Sorry, didn't read carefully (or the whole thread).

Bert is right:
Smalltalk classNamed: 'Object'

On 1/16/07, David Mitchell <[hidden email]> wrote:

> The method Object>>#class does what you want. Since it is defined on
> Object you can send the message to any object, including a string.
>
> > returnSuperClass: aClassName
> > |aSuperClass|
> >
> > aSuperClass := (Smalltalk classNamed: aClassName) superclass.
> >
> > ^ aSuperClass.
> >
>
And now the one liner:
^(Smalltalk classNamed: aClassName) superclass.
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners