Skip to content
Abe Pralle edited this page Sep 7, 2022 · 5 revisions

Syntax

class Name<<Template-Parameters>>( constructor-properties ) : Base-Types [attributes]
  NATIVE
    # Native C code
    nativeHeader @|<C code injected into .h header>
                  |<continuation of previous line>

    nativeCode   @|<C code injected into .c source>

  GLOBAL PROPERTIES
    # AKA static variables or class variables.
    name [= initial-value] [, ...] [: Type]

  GLOBAL METHODS
    # AKA static methods or class methods.
    method create(...)->Name
      # Factory constructor.

    method name
      ...

    method name( arg:Type, property-name, arg=default:Type, &flag )->ReturnType
      ...
      return xyz

  PROPERTIES
    # AKA instance variables.
    name [= initial-value], ... [: Type]
    x = 5 : Int32  # example
    native "C code;"

  METHODS
    # AKA instance methods or member methods.
    method init
      # Default constructor.

    method init( ... )
      # Overloaded constructor.

    method on_cleanup
      # Optional, called when an object is about to be deleted.

    method name
      ...

    method name( arg:Type, property-name, arg=default:Type, &flag )->ReturnType
      ...
      return xyz

    method set_x( new_x:Int32 )
      # Optional setter method for property 'x'. Called when you write "x = value" or obj.x = value".
      @x = new_x   # use '@' for direct access to 'x'; otherwise this would be an infinite recursion.

    method x->Int32
      # Optional getter method for property 'x'.
      return @x  # use '@' for direct access to 'x' within this getter.
endClass

class Declaration

Simplest Form

class ABC
endClass

class ABC;  # alternative

local obj = ABC()

Simple Classes With Properties

class Int32Pair
  PROPERTIES
    a : Int32
    b : Int32

  METHODS
    method init( a, b )
endClass

class Real64Pair
  PROPERTIES
    a : Real64
    b : Real64

  METHODS
    method init( a, b )
endClass

local pair = Int32Pair(3,4)
pair.a += pair.b
++pair.b

Simple Classes With Constructor Properties

class Int32Pair( a:Int32, b:Int32 )
endClass

class Real64Pair( a:Real64, b:Real64 );

local pair = Int32Pair(3,4)
pair.a += pair.b
++pair.b

Class Templates

class Pair<<$Type>>( a:$Type, b:$Type );

local pair = Pair<<Int32>>(3,4)
pair.a += pair.b
++pair.b

Base Types (Extended Classes)

class Pet( name:String )
  METHODS
    method speak
      println "$ makes a noise"(name)
endClass

class Cat : Pet
  METHODS
    method speak
      println "$ meows"(name)
endClass

local pet = Cat("Fluffy")
pet.speak  # Fluffy meows

println pet instanceOf Cat  # true
println pet instanceOf Pet  # true
println pet instanceOf Dog  # false

Attributes

Attribute Description
abstract Class cannot be instantiated directly. Only extended classes can be instantiated. A class is most often [abstract] when it contains [abstract] methods.
api An [api] class is implicitly [essential] and in addition it marks all of its methods [essential] as well.
aspect Class defines an aspect instead of a regular object. An aspect is like a Java interface with properties and method definitions; class elements are "copy-pasted" into an extended class when it incorporates that aspect. See Aspects.
compound Class is a compound (pass-by-value) type.
essential An essential class is included in the compiled output even if it is unused, normally because linked code will call methods of the essential class. Furthermore essential singletons are initialized when the program launches instead of waiting until the singleton is first referenced.
primitive Designates this class as a primitive type. This is for internal use only; new primitive types cannot be added without modifying the RogueC compiler.
singleton Automatically creates a global singleton reference for an object of this class. When the reference is first accessed, an object of this class is created (if necessary) and assigned to the reference. Singletons can be reassigned to be any extended class object; they can also be assigned null.

Singletons

class XYZ [singleton]
  GLOBAL METHODS
    method on_singleton_change( old_singleton:XYZ, new_singleton:XYZ )
      # No actions are required here but this is an opportunity to make any
      # incidental changes that may be required. Either reference may be null.

  METHODS
    method init
      # Automatically called when the singleton is initialized.

    method alpha
      ...
endClass

XYZ.alpha   # XYZ singleton initialized.
XYZ.beta    # Same singleton object used.
XYZ = XYZ() # A different object will be used as the XYZ singleton.

When an object class has the [singleton] attribute (class XYZ [singleton]), the name of the class can be used as a reference to a singleton object - a globally available single instance of the class.

When a singleton is first referenced (XYZ.alpha = 5), the singleton object is created. An init() constructor will be called if defined. Note that a constructor with default args (init(n=10:Int32)) won't work as the singleton constructor.

If a class is an [essential singleton] then the singleton will be automatically instantiated at the beginning of the program rather than waiting for the first access.

Singletons can be reassigned to be different objects if desired: XYZ = XYZ().

If a class defines a global method on_singleton_change(old_singleton:Type,new_singleton:Type), that method is called any time the singleton reference changes. Note that the old and/or the new reference parameter can be null.

GLOBAL PROPERTIES

class MyClass
  GLOBAL PROPERTIES
    x : Int32
...
MyClass.x = y

Any variable defined in a GLOBAL PROPERTIES section has the same value for all objects of the class and can be accessed using the class name instead of an object reference. Some languages call these "static variables" or "class properties".

GLOBAL METHODS

class Geometry
  GLOBAL METHODS
    method hypotenuse( x:Real64, y:Real64 )->Real64
      return (x*x + y*y).sqrt
...
println Geometry.hypotenuse( 3, 4 )  # 5.0

Like global properties, methods defined in a GLOBAL METHODS section can be called on a class name instead of an object reference. They can access global properties but not object properties. Some languages call these "static methods" or "class methods".

See also: GLOBAL METHODS

PROPERTIES

class XY
  PROPERTIES
    x : Real64
    y : Real64
endClass

local xy = XY()
xy.x = 3
xy.y = 4

A class PROPERTIES section defines the properties (object properties AKA instance variables) that belong to every instantiated object of that class.

METHODS

class XY( x:Real64, y:Real64 )
  METHODS
    method hypotenuse->Real64
      return Geometry.hypotenuse(x,y)
endClass

local xy = XY(3,4)
println xy.hypotenuse # 5.0

A class METHODS section defines methods (object methods) that can be called on every instantiated object of that class.

See also: METHODS

Property Getters

class String
  PROPERTIES
    hash_code : Int32?

  METHODS
    method hash_code->Int32
      if (not @hash_code) calculate_hash_code
      return @hash_code.value
...
println "Hello World".hash_code

A property getter is a method with the same name as an existing property (or even a non-existent property). Any standard reading of that property results in the getter method being called to compute the result. Use direct property access (@property_name) to bypass a property getter.

Property Setters

class Progress( percent=0:Int32 )
  METHODS
    method set_percent( new_value:Int32 )
      @percent = new_value.clamped(0,100)
...
local progress = Progress()
progress.percent = 110
println progress.percent  # 100

A property setter is a method that begins with set_ and ends with the same name as an existing property (or even a non-existent property). Any standard assignment to that property results in the setter method being called to store the result. Use direct property access (@property_name = value) to bypass a property setter.

Direct Property Access

println @property_name     # bypasses any property getter
println obj.@property_name
@property_name = x         # bypasses any property setter
obj.@property_name = x
Clone this wiki locally