I’ll start by prefacing that I think operator overloading is good iff it’s implemented in a way that a single operator has only one, well-defined meaning
2024-02-12 this means
+
really means addition and nothing else.2024-02-12 the way I’d like to do it in my dream language is by a few means
2024-02-12 (+)
is defined to be a polymorphic operator which calls into a module implementing theAddSub
interface, which means you have to implement both addition and subtraction for(+)
to work on your typelet AddSub = interface { type T fun add (a : T) (b : T) : T fun subtract (a : T) (b : T) : T } fun (+) (a : let T) (b : T) : T use AS : AddSub with { T } = AS.add a b
2024-02-12
so now getters and setters: what’s so bad about them?
2024-02-12 not to mention all the infinite recursion annoyance that sometimes happens when implementing them manually
2024-02-12 this is less of a problem in languages that feature automatic generation of getters and setters - such as Kotlin
var someVariable: String get private set // no infinite recursion to be seen here!
but it’s still an issue in e.g. JavaScript, where one mistake can send your call stack down the spiral:
class Example { #someVariable = ""; get someVariable() { return this.someVariable; } // typo!!!! set someVariable(value) { this.someVariable = value; } // typo again!!!!!!!!!! dammit! }
and the error is not caught until runtime.
2024-02-12