Skip to content

Inline Initializers

AbePralle edited this page May 5, 2015 · 3 revisions

Syntax

class ClassName( new_property:Type, inherited_property, ... ) 
    : BaseClassName, ... [attributes]

Example

class PointTest
  METHODS
    method init
      local pt1 = Point2D(3,4)
      local pt2 = Point3D(5,6,7)
      println pt1  # prints (3,4)
      println pt2  # prints (5,6,7)
endClass

class Point2D( x:Integer, y:Integer )
  METHODS
    method to->String
      return "($,$)" (x,y)
endClass

class Point3D( x, y, z:Integer ) : Point2D
  METHODS
    method to->String
      return "($,$,$)" (x,y,z)
endClass

Description

Each class definition can have an inline initializer immediately following the class name. The inline initializer lists a mix of inherited property names (without type declarations) and new property names (with type declarations) and implicitly creates an init() method with listed property arguments.

Notes

  1. Additional PROPERTIES and overloaded init() methods can be defined normally.
  2. There is no way to add additional statements into the implicitly created init() method - you'd have to replace the inline initializer with separate property and method declarations.
  3. [compound] classes require the inline initializer syntax to specify the properties that compose the compound.

Clone this wiki locally