Wiki

Changes between Version 1 and Version 2 of MethodInvocation

Show
Ignore:
Timestamp:
06/26/13 10:36:28 (11 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • MethodInvocation

    v1 v2  
    1 = Method Invocation = 
     1= Method Invocation and Instance Creation = 
     2 
     3== Class Instance Creation == 
     4Instances 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 
     9ci = MyClass('neko')  # ci is an instance of MyClass 
     10assert c.name == 'neko' 
     11 
     12ci = MyClass()   # error - no corresponding no-arg initializer 
     13ci = MyClass(99) # error - no corresponding (1 int param) initializer 
     14 
     15class MyClass 
     16    var name = '' 
     17 
     18    def init(n as String) 
     19        base.init 
     20        .name = n 
     21    # ... 
     22 
     23}}} 
     24 
     25Unlike 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 
    238 
    339TODO 
    440 
    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   
    1244   * Methods without params dont require trailing empty () - instance.methodName 
    13    * Methods declared without return type have void returnType, cannot be assigned to anything  
     45   * Methods declared without return type have a '''void''' returnType, cannot be assigned to anything  
     46   * Methods declared without access modifiers have  '''public''' access - accessible to everything  
    1447 
    1548Method references 
    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 (). 
    2053   * example: 
    2154