jesseduffield/OK

equals example is dangerous and confusing

timthelion opened this issue · 1 comments

As you know, most modern programming languages contain at least two and sometimes three equals operators. This is because value equality and identity comparison are type specific and open up a whole can of worms.

It therefore surprised me when you suggested defining equals as:

let equals = fn(a, b) {
  let x = a >= b;
  let y = lazy b >= a;
  return x && y;
}

This seems to over-complicate the issue by either assuming generics or duck typing? Generics are scary!

May I suggest instead defining equals for each type independently as follows:

let equals = fn(a : int, b: int) {
  let x = a >= b;
  let y = lazy b >= a;
  return x && y;
}
let equals = fn(a : string, b: string) {
  let x = a >= b;
  let y = lazy b >= a;
  return x && y;
}
let equals = fn(a : char, b: char) {
  let x = a >= b;
  let y = lazy b >= a;
  return x && y;
}
let equals = fn(a : Person, b: Person) {
  let x = a >= b;
  let y = lazy b >= a;
  return x && y;
}

This eliminates confusing ideas like duck typing (should we call it duck taping?) and scary topics like generics. Someone died once due to buying cheep generic medicine online!

PS: Why is y lazy?