class Platform cue init(name as String) base.init _name = name get name from var as String def equals(other as Object?) as bool is override if other is this, return true if other inherits Platform return .name == other.name else return false def getHashCode as int is override return .name.getHashCode def toString as String is override return .name sig ArithmeticOp(a as int, b as int) as int sig SimpleMethod class Test def main .testDelegates .testEvents get two as int return 2 def testDelegates nonsorted = [Platform('Linux'), Platform('MS Windows'), Platform('Apple')] sorted = [Platform('Apple'), Platform('Linux'), Platform('MS Windows')] assert nonsorted <> sorted # sort with ref t = List(nonsorted) assert t == nonsorted and t <> sorted t.sort(ref .comparePlatforms) assert t == sorted and t <> nonsorted # sort with anonymous method with explicitly typed arguments and return type t = List(nonsorted) assert t == nonsorted and t <> sorted t.sort(do(a as Platform, b as Platform) as int) return a.name.compareTo(b.name) assert t == sorted and t <> nonsorted # sort with anonymous method with explicitly typed arguments and implicit return type t = List(nonsorted) assert t == nonsorted and t <> sorted t.sort(do(a as Platform, b as Platform)) return a.name.compareTo(b.name) assert t == sorted and t <> nonsorted # again but with a comment at the top of the block t = List(nonsorted) assert t == nonsorted and t <> sorted t.sort(do(a as Platform, b as Platform)) # comment return a.name.compareTo(b.name) assert t == sorted and t <> nonsorted /# # sort with anonymous method with implicitly typed arguments and implicit return type Not supported by C# or Cobra at this point. Might be nice to have. t = List(nonsorted) assert t == nonsorted and t <> sorted t.sort(do(a, b)) return a.name.compareTo(b.name) assert t == sorted and t <> nonsorted #/ # use anonymous method in a more complex statement assert 12 == .callArithmeticOp(3, 4, do(a as int, b as int)) return a * b # multiple statements in the anonymous method assert 12 == .callArithmeticOp(3, 4, do(a as int, b as int)) r = 0 for i in a r += b assert r == a * b return r # access members assert 24 == .callArithmeticOp(3, 4, do(a as int, b as int)) return a * b * .two # access local vars multiplier = 10 assert 120 == .callArithmeticOp(3, 4, do(a as int, b as int)) return a * b * multiplier # no parameters count = 0 .invoke(do) count += 1 assert count == 1 .invoke(do()) # .warning. unnecessary parentheses count += 1 # inside a control statement if true t = List(nonsorted) assert t == nonsorted and t <> sorted t.sort(do(a as Platform, b as Platform)) return a.name.compareTo(b.name) assert t == sorted and t <> nonsorted /# # comma block count = 0 .invoke(do), count += 1 error: Comma-separated assignment targets must end with "=", or this is a general syntax error. assert count == 1 #/ def comparePlatforms(a as Platform, b as Platform) as int return a.name.compareTo(b.name) def callArithmeticOp(a as int, b as int, op as ArithmeticOp) as int return op(a, b) def invoke(method as SimpleMethod) method() def testEvents # with an event _clickCount = 0 listen .onClick, do _clickCount += 1 assert _clickCount == 0 raise .onClick assert _clickCount == 1 var _clickCount as int event onClick as EventHandler