[Newbies] invoking a *class* method from a workspace
mstram
mstramba at sympatico.ca
Sat May 26 16:32:24 UTC 2007
Well obviously I need to a lot more reading and experimenting with this,
even if "class" methods are "regular" methods, there is something different
with them as far as :
1) the syntax to access them ("fire" .. "call" .. "invoke" ??? what is the
traditional term ? )
2) At least one difference I've found is that I when tried to create a
"class" method that accessed an instance variable, Squeak warned that the
variable didn't exist and asked if I wante to create it (is this a bug?) ...
even though it was "declared" in the class definition.
I tried modifying my MikeTest to :
Object subclass: #MikeTest
instanceVariableNames: 'ivar1'
classVariableNames: 'Cvar1'
poolDictionaries: ''
category: 'Mike1'
and adding :
A) -----------"instance method " -------------------------------
imet: ivar icmet: cvar
ivar1 := ivar.
Cvar1 := cvar.
^ 'I am an instance method'
---------------------------------------------
iget
|temp|
temp := Dictionary new.
temp at: 'ivar1' put: ivar1.
temp at: 'cvar1' put: Cvar1.
^ temp.
and
B) --------------"class method " --------------------------------
cmet: ivar ccmet: cvar
ivar1 := ivar.
Cvar1 := cvar.
Cvar1 := cvar.
^ 'I am a Class Method'
--------------------------------------------------------------
cget
|temp|
temp := Dictionary new.
temp at: 'cvar1' put: Cvar1.
temp at: 'ivar1' put: ivar1.
^ temp.
=================================
(I realize 'iget' and 'cget' are the same thing, I just wanted to see if
there is any difference when running them.)
But Squeak says that 'ivar1' is undeclared and asks if I want to declare it.
It accepts it being declared as an instance variable (even though it already
was according to the class definition ... (is this a bug?).
I can set / retrieve the variables with either the "class" or "instance"
methods, so I'm not sure why Classes have been setup in Squeak using both
types of methods.
t1 := MikeTest new.
result := MikeTest cmet: 'Class method setting inst var ivar1-> inst44'
ccmet: 'Class method setting class var Cvar1 -> Class01'
Bert Freudenberg-2 wrote:
>
> On May 26, 2007, at 14:50 , mstram wrote:
>
>> What's the trick / catch ... if there is one for invoking a class
>> method from
>> a workspace?
>
> "Class methods" in Smalltalk are regular methods, because classes are
> regular objects. Unfortunately the browser gives the impression that
> there are two kinds of methods ...
>
> MyClass new
>
> simply sends #new to the object MyClass. This is how you invoke a
> "class method".
>
> Remember, anything in Smalltalk is an object. Even classes. There is
> not even a "syntax" to define a class or a method (just like there is
> no syntax for control structures). Everything is accomplished by
> sending messages.
>
> - Bert -
>
>
--
View this message in context: http://www.nabble.com/invoking-a-*class*-method-from-a-workspace-tf3820452.html#a10817944
Sent from the Squeak - Beginners mailing list archive at Nabble.com.
More information about the Beginners
mailing list