Version 2 (modified by hopscc, 9 years ago)


Method Invocation and Instance Creation

Class Instance Creation

Instances of a Type (Class or Structure) are created by invoking the class (Type) name, passing arguments as given in any of the classes initializer methods. The initializer to invoke is chosen by finding the best match to the number and types of parameters given in the Type initializer method(s).

ci = MyClass('neko')  # ci is an instance of MyClass
assert == 'neko'

ci = MyClass()   # error - no corresponding no-arg initializer
ci = MyClass(99) # error - no corresponding (1 int param) initializer

class MyClass
    var name = ''

    def init(n as String)
        .name = n
    # ...

Unlike method invocation, even if the Type has a no-arg constructor you must still explicitly specify an empty argument list. (to distinguish between a instance creation and obtaining a reference to the named Type)

  ci = NoArgClass() 
  # ci is an instance of NoArgClass

  # NOT xc = NoArgClass 
  # this makes xc a reference to the type NoArgClass
  # same as above construction followed by xc = ci.typeOf 


Invoking Methods

  • call as instance.methodName(arg, arg, arg...)
  • call as Class.methodName(arg, arg, arg...) for class/shared methods (stylistic) though can access through an instance
  • Methods without params dont require trailing empty () - instance.methodName
  • Methods declared without return type have a void returnType, cannot be assigned to anything
  • Methods declared without access modifiers have public access - accessible to everything

Method references

  • ref instance.methodName - Obtain a (instance and) method reference,
  • pass to listen/ignore statement or store in a variable of the appropriate type ( typed by a sig - method Signature)
  • call/invoke using variable name with () following enclosing the call arglist
  • even if no args passed still requires ().
  • example:

Anon Methods/Closures

  • Captures any local variables in calling scope if used in anon method
  • 'do' keyword
  • anon method/closure code indented on following lines
  • do
    • anon method code
  • do(param, param...)
    • anon method code


  • one line (one expression) anon method
  • do = EXPR
  • do(<param, param...) = EXPR

What else ?


See Also

Back to LanguageTopics