class Foo cue init(i as int, s as String) base.init _i, _s = i, s pro i from var as int pro s from var as String # Cobra will invoke a strongly typed .equals method # for == and <> between two statically typed objects: def equals(other as Foo) as bool return other.i == .i and other.s == .s # Override Object.equals # Cobra will use this in dynamic situations # and the .NET libs may use this in others, # such as using Foo as a key in a dictionary: def equals(obj as Object?) as bool is override if obj inherits Foo, return .equals(obj) return false # When you override Object.equals, you should override .getHashCode: def getHashCode as int is override return HashCodeUtils.combine(.i.getHashCode, .s.getHashCode) class Program def main assert Foo(5, 'foo') == Foo(5, 'foo') assert Foo(5, 'foo') <> Foo(2, 'foo') |
How To Customize Object Equality