[Newbies] Re: Instance Creation Howto

stéphane ducasse ducasse at iam.unibe.ch
Mon Aug 14 19:07:22 UTC 2006


Hi

this solution is not really adequate since initializer is an instance  
method therefore the ComplexClass new is created but to receive
the message initializer we need to already have an instance. :)

Stef

On 14 août 06, at 16:18, Thiyagarajan wrote:

> hello
>
>
> aComplex := ComplexClass new.
>
> and create a method "new" which override new
>
> new
>     ^ super new initializer
>
> and create a another method "initializer"
>
> initializer
>     | aVariable |
>     aVariable := ComplexClass new.
>     aVariable firstVariable: 22.
>     aVariable secondVariable: 23.
>     ^aVariable
>
>
>
>
>
> ------------------------------
>
> Message: 10
> Date: Sun, 13 Aug 2006 23:23:43 +0200
> From: hypolit <hypolit at googlemail.com>
> Subject: [Newbies] Instance Creation Howto
> To: beginners at lists.squeakfoundation.org
> Message-ID:
>         <e4dc659c0608131423pfd5ecbak97309d93234a0261 at mail.gmail.com >
> Content-Type: text/plain; charset="iso-8859-1"
>
> Hello,
>
> I am new to Smalltalk and Squeak in general. After having started  
> to read
> "Inside Smalltalk" and "Smalltalk by Example" i came along two similar
> examples, which sound quite reasonable but don't seem to work in  
> Squeak.
> I want to create an instance of my own class "ComplexClass".
>
> It is defined like this:
> Object subclass: #ComplexClass
>     instanceVariableNames: 'firstVar secondVar'
>     classVariableNames: ''
>     poolDictionaries: ''
>     category: 'TestClass'
>
>
> I can create instances with
> aComplex := ComplexClass new.
> normally.
>
> But what if i wanted to call another method instead of "new" to  
> create an
> instance?
> Take, for example, my method "initializer":
>
> initializer
>     | aVariable |
>     aVariable := ComplexClass new.
>     aVariable firstVariable: 22.
>     aVariable secondVariable: 23.
>     ^aVariable
>
>
> In both books there are methods quite similar to my "initializer"  
> method.
> But if execute:
>
> aComplex2 := ComplexClass initializer.
>
> I get an error message, something like "Undefined Object does not  
> understand
> #initializer".
> Do i have to write:
>
> aComplex2 := ComplexClass new.
> aComplex2 := aComplex2 initalizer.
>
> That would work but it is quite ugly.
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners



More information about the Beginners mailing list