6.25.3.7 Method conveniences

In a method you usually access the receiving object pretty often. If you define the method as a plain colon definition (e.g., with :noname), you may have to do a lot of stack gymnastics. To avoid this, you can define the method with m: ... ;m. E.g., you could define the method for drawing a circle with

m: ( x y circle -- )
  ( x y ) this circle-radius @ draw-circle ;m

When this method is executed, the receiver object is removed from the stack; you can access it with this (admittedly, in this example the use of m: ... ;m offers no advantage). Note that I specify the stack effect for the whole method (i.e. including the receiver object), not just for the code between m: and ;m. You cannot use exit in m:...;m; instead, use exitm.34

You will frequently use sequences of the form this field (in the example above: this circle-radius). If you use the field only in this way, you can define it with inst-var and eliminate the this before the field name. E.g., the circle class above could also be defined with:

graphical class
  cell% inst-var radius

m: ( x y circle -- )
  radius @ draw-circle ;m
overrides draw

m: ( n-radius circle -- )
  radius ! ;m
overrides construct

end-class circle

radius can only be used in circle and its descendent classes and inside m:...;m.

You can also define fields with inst-value, which is to inst-var what value is to variable. You can change the value of such a field with [to-inst]. E.g., we could also define the class circle like this:

graphical class
  inst-value radius

m: ( x y circle -- )
  radius draw-circle ;m
overrides draw

m: ( n-radius circle -- )
  [to-inst] radius ;m
overrides construct

end-class circle

Footnotes

(34)

Moreover, for any word that calls catch and was defined before loading objects.fs, you have to redefine it like I redefined catch: : catch this >r catch r> to-this ;