Add equality
This commit is contained in:
parent
e7f68a4647
commit
d641fc23c0
|
@ -0,0 +1,54 @@
|
||||||
|
actor Main
|
||||||
|
new create(env: Env) =>
|
||||||
|
// identity equality: there is only one None
|
||||||
|
// so None `is` None
|
||||||
|
if None is None then
|
||||||
|
env.out.print("None is None")
|
||||||
|
end
|
||||||
|
|
||||||
|
let a = Foo("hi")
|
||||||
|
let b = Foo("hi")
|
||||||
|
|
||||||
|
// a and b are different objects
|
||||||
|
if a is b then
|
||||||
|
env.out.print("a is b")
|
||||||
|
else
|
||||||
|
env.out.print("a is not b")
|
||||||
|
end
|
||||||
|
|
||||||
|
let c = a
|
||||||
|
|
||||||
|
// c is a reference to the same object to which
|
||||||
|
// a is a reference
|
||||||
|
// I assume? I don't actually know how the memory
|
||||||
|
// management works lol
|
||||||
|
if a is c then
|
||||||
|
env.out.print("a is c")
|
||||||
|
else
|
||||||
|
env.out.print("a is not c")
|
||||||
|
end
|
||||||
|
|
||||||
|
// structural equality: see fun eq() below
|
||||||
|
if a == b then
|
||||||
|
env.out.print("a is equal to b")
|
||||||
|
end
|
||||||
|
if a == c then
|
||||||
|
env.out.print("a is equal to c")
|
||||||
|
end
|
||||||
|
|
||||||
|
class Foo
|
||||||
|
let field: String
|
||||||
|
|
||||||
|
new create(field': String) =>
|
||||||
|
field = field'
|
||||||
|
|
||||||
|
// the value returned by this function implements
|
||||||
|
// structural equality
|
||||||
|
fun eq(that: box->Foo): Bool =>
|
||||||
|
this.field == that.field
|
||||||
|
|
||||||
|
|
||||||
|
// Extra note about primitives: since all primitives have no fields and there
|
||||||
|
// is only one instance of each user-defined primitive, all primitives of a
|
||||||
|
// given type are equal by identity and structurally equal. (I'm not sure
|
||||||
|
// about builtin primitives.)
|
Loading…
Reference in New Issue