common

A common attribute must be a function, and can not be modified, and its code is therefore shared by all members of the same class. When declaring a common attribute, the default value (a function definition) must be provided, and will become the attribute value of all members of the class.

In the previous example, the two functions getRadius and setRadius are common attributes. Common attributes as functions can be called outside or inside the class, but they cannot be modified. For example

>>  circle = class 
      ...
      common  setRadius = function x  ->  ()
        if x > 0
            parent.radius = x;
         end
      end
      ...
    end
>>  p = circle.new();  
>>  p.setRadius = function x -> ()
         ...
    end

    Error: modifying common attribute
If setRadius is declared as public, then we can still use it almost the same way, but we'll be free to assign new values to the setRadius attribute of any individual member of circle.



oz 2009-12-22