Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can getter/setter code be generated automatically for a class in Pharo or Squeak?

I have a long list of instance variables to create for a class that I want to generate the code for, rather than do it by hand. The list comes from an existing SQL database. My intention is to do it all in a pure object-oriented way with Smalltalk first, and as I learn more, save the data back to the database and work from it directly.

Is there a way of passing the list of names to method that will generate them and add them to the class definition?

In fact is there a way of adding or modifying class definitions dynamically in Smalltalk? I suspect there must and I would like to know a best practices approach.

Update: What I have in mind is more like passing a list of the instance variables to a method that will create them automatically.

It is more like:

addVariablesAndAccessors className: MyClass variablesList: ('aaaa', 'bbbb', 'cccc')

which will then result in a call to

AddVariables className: MyClass variableList: ('aaaa' 'bbbb' cccc')

and

generateAccessors className: MyClass variableList: ('aaaa' 'bbbb' cccc')

like image 759
vfclists Avatar asked Jan 09 '13 11:01

vfclists


People also ask

How do getter and setter methods work?

The getter method returns the value of the attribute. The setter method takes a parameter and assigns it to the attribute. Getters and setters allow control over the values. You may validate the given value in the setter before actually setting the value.

Which keyword is used to create a getter method in class?

All classes have a default getter method but it can be overridden explicitly. The getter method can be defined using the get keyword as: return_type get field_name{ ... }

Do getters come before setters?

getter/setter pairs. first getters, then setters (or the other way around)


1 Answers

In OmniBrowser with the refactoring tools loaded you select the class and in the context menu Refactor class > Accessors.

Alternatively, if you only want to create an accessor for a single variable, select Refactor instance/class variable > Accessor, and select the variable you want to access.

like image 98
Lukas Renggli Avatar answered Sep 24 '22 23:09

Lukas Renggli