1 | | = Method Invocation = |
| 1 | = Method Invocation and Instance Creation = |
| 2 | |
| 3 | == Class Instance Creation == |
| 4 | 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). |
| 5 | |
| 6 | |
| 7 | {{{ |
| 8 | #!cobra |
| 9 | ci = MyClass('neko') # ci is an instance of MyClass |
| 10 | assert c.name == 'neko' |
| 11 | |
| 12 | ci = MyClass() # error - no corresponding no-arg initializer |
| 13 | ci = MyClass(99) # error - no corresponding (1 int param) initializer |
| 14 | |
| 15 | class MyClass |
| 16 | var name = '' |
| 17 | |
| 18 | def init(n as String) |
| 19 | base.init |
| 20 | .name = n |
| 21 | # ... |
| 22 | |
| 23 | }}} |
| 24 | |
| 25 | Unlike method invocation, even if the Type has a no-arg constructor you must still explicitly specify an empty argument list. |
| 26 | (to distinguish between a instance creation and obtaining a reference to the named Type) |
| 27 | {{{ |
| 28 | #!cobra |
| 29 | ci = NoArgClass() |
| 30 | # ci is an instance of NoArgClass |
| 31 | |
| 32 | # NOT xc = NoArgClass |
| 33 | # this makes xc a reference to the type NoArgClass |
| 34 | # same as above construction followed by xc = ci.typeOf |
| 35 | }}} |
| 36 | |
| 37 | |
5 | | Class Creation |
6 | | * Instance creation calls the matching class initializer ( matches on arglist and types) |
7 | | * !ClassName() - no args |
8 | | * !ClassName( arg,arg,....) |
9 | | |
10 | | Class Methods |
11 | | * call as instance.methodName(args, arg, arg...) |
| 41 | == Invoking Methods == |
| 42 | * call as instance.methodName(arg, arg, arg...) |
| 43 | * call as Class.methodName(arg, arg, arg...) for class/shared methods (stylistic) though can access through an instance |
16 | | * ref methodName - Obtain a method reference, |
17 | | * pass to listen/ignore statement or store in a variable of the appropriate type ( sig - method Signature) |
18 | | * call/invoke using variable name with () following enclosing arglist |
19 | | * even if no args requires () |
| 49 | * ref instance.methodName - Obtain a (instance and) method reference, |
| 50 | * pass to listen/ignore statement or store in a variable of the appropriate type ( typed by a sig - method Signature) |
| 51 | * call/invoke using variable name with () following enclosing the call arglist |
| 52 | * even if no args passed still requires (). |