Michael
Michael

Reputation: 5072

Pharo: Refers to class name instead of "self class"

I have a simple class Amount with the fields value and unit and corresponding accessors. Now I want to create a class method for constructing amount objects like this:

value: aValue unit: anUnit
| amount |
amount := Amount new .
amount value: aValue ; unit: anUnit .
^ amount

I get the warning message Refers to class name instead of "self class". How can I improve this? I tried amount := (self class) new but then I get the error A Metaclass should only have one instance!. (Note that I am very new to Pharo and Smalltalk)

Upvotes: 2

Views: 782

Answers (1)

MartinW
MartinW

Reputation: 5041

Refers to class name instead of "self class"

is a hint, that suggests you use

amount := self new.

instead of

amount := Amount new.

As your method is a class method, self refers to the class.

In a class method, (self class) new would (in your example) be the same as Amount class new. The class of a class (Amount) is a Metaclass, thus the error message you quoted.

Upvotes: 3

Related Questions