Adding instance variables during runtime

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

Adding instance variables during runtime

Warakorn Paphrawat
In my current little project I would like write classes in Smalltalk. However, when I am designing the classes I do not know, which instance variables the class should have.

While I am running the application, the user (or another agent) determines which instance variables should be assigned to a class.
How can I add instance variables and possibly class variables to a class, while the program is running ? Actually how can the Smalltalk code be automatically transformed (and "accepted") from the 'BEFORE' state to the 'AFTER' state.

// BEFORE //
Object subclass: #Class1
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    category: 'some-category'

// AFTER //
Object subclass: #Class1
    instanceVariableNames: 'InstVar1 InstVar2 '
    classVariableNames: 'ClassVar1 ClassVar2'
    poolDictionaries: ''
    category: 'some-category'

I worked with the ClassBuilder class, but I haven't achieved this kind of automated code transformation.
Let's assume the desired variable names are available as normal strings and should be added one after another (method like: addNewInstanceVariable: aString ).

Don't worry about getter/setter methods for the new instance variables, that's another issue.


Thanks in advance
Warakorn


Want to start your own business? Learn how on Yahoo! Small Business.
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Adding instance variables during runtime

Mathieu SUEN
Look at Class>>addInstVarName:

Point addInstVarName: 'x'.

Warakorn Paphrawat a écrit :

> In my current little project I would like write classes in Smalltalk.
> However, when I am designing the classes I do not know, which instance
> variables the class should have.
>
> While I am running the application, the user (or another agent)
> determines which instance variables should be assigned to a class.
> How can I add instance variables and possibly class variables to a
> class, while the program is running ? Actually how can the Smalltalk
> code be automatically transformed (and "accepted") from the 'BEFORE'
> state to the 'AFTER' state.
>
> // BEFORE //
> Object subclass: #Class1
>     instanceVariableNames: ''
>     classVariableNames: ''
>     poolDictionaries: ''
>     category: 'some-category'
>
> // AFTER //
> Object subclass: #Class1
>     instanceVariableNames: 'InstVar1 InstVar2 '
>     classVariableNames: 'ClassVar1 ClassVar2'
>     poolDictionaries: ''
>     category: 'some-category'
>
> I worked with the ClassBuilder class, but I haven't achieved this kind
> of automated code transformation.
> Let's assume the desired variable names are available as normal strings
> and should be added one after another (method like:
> addNewInstanceVariable: aString ).
>
> Don't worry about getter/setter methods for the new instance variables,
> that's another issue.
>
>
> Thanks in advance
> Warakorn
>

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

Re: Adding instance variables during runtime

cedreek
2006/12/12, Mathieu <[hidden email]>:
> Look at Class>>addInstVarName:
>
> Point addInstVarName: 'x'.
>

It's possible but not recommended (I think)

What you could try is to use a container as instVar (or classInstVar)
in which you can add several elements that will act as instVar.
It's a model called AOM (Adaptive Object Model)

have a look here: http://www.adaptiveobjectmodel.com/

Cédrick

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