Instance Creation Howto

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

Instance Creation Howto

hypolit
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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Instance Creation Howto

stéphane ducasse-2
are you sure that your new2 method (initializer) is on the class side?
If this method should be sent to a class then it should be defined on  
the class side (click on the class button)

ComplexClass new2 is sent to the class ComplexClass itself so it  
should be defined on the class side
of the Complex class.

Stef

On 13 août 06, at 23:23, hypolit wrote:

> 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
> [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: Instance Creation Howto

hypolit
Well, i defined it on the instance side, of course.
Quite stupid.


Thank you very much for your fast reply!

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

Re: Instance Creation Howto

stéphane ducasse-2

On 13 août 06, at 23:42, hypolit wrote:

> Well, i defined it on the instance side, of course.
> Quite stupid.

:)
not really. Try to really understand it because in Smalltalk there is  
only one single model and msg passing schema.
nice but sometimes complex to grasp at first
>
>
> Thank you very much for your fast reply!

You mean ultra fast I hope :)

Stef

> _______________________________________________
> 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: Instance Creation Howto

Thiyagarajan-2
In reply to this post by hypolit
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 <[hidden email]>
Subject: [Newbies] Instance Creation Howto
To: [hidden email]
Message-ID:
        <[hidden email]>
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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Re: Instance Creation Howto

stéphane ducasse-2
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 <[hidden email]>
> Subject: [Newbies] Instance Creation Howto
> To: [hidden email]
> Message-ID:
>         <[hidden email] >
> 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
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

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