jesseduffield / OK

Welcome to the future of programming languages: OK?
MIT License
529 stars 19 forks source link

equals example is dangerous and confusing #15

Open timthelion opened 2 years ago

timthelion commented 2 years ago

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!

timthelion commented 2 years ago

PS: Why is y lazy?