3 ms·
> implement the join function by adding an extension on `Array<String>`. And the + function would just be, for example, `static func + (left: Vector2D, right: V
by ssivark 4y ago
> implement the join function by adding an extension on `Array<String>`. And the + function would just be, for example, `static func + (left: Vector2D, right: Vector2D) -> Vector2D`.
1. We are talking about a function that takes two values and spits out a third. In what sense are we talking about calling a method (or passing a message) to an object (the first string) to mutate its state? How do we enable a compiler to reason about the side-effects (or lack) of this code?
2. In mathematics "+" is almost always used for a commutative operation on values, so defining it the way you have breaks an (implicit) interface that is not explicitly codified (in part because current languages do not give us a way to codify this). The ability to reason about such properties is crucial if you want the runtime to be able to automatically reorder/parallellize/etc without breaking correctness.
3. While these details are minor annoyances in the case of string concatenation (fairly benign problem), they can become really painful when you are trying to leverage the power of generic programming to compose sophisticated algorithms. A classic poster example for this is when you want to add units or uncertainties/error-bars to your numbers and have all your numerical & plotting code generalize seamlessly -- Julia (for example) performs beautifully, while I don't see how any single dispatch (OOP) language can handle this challenge. More examples in this spirit: complex numbers, autodiff with dual numbers, etc.
- RivieraKid 4y ago1. We're not talking about a method that mutates state because this method doesn't mutate state. We can add a `mutating` keyword to the method (this is in fact implemented for Swift structs, but not classes). 2. I don't get this. What's the difference from Julia? Whether the operation is commutative is defined by the implementation of +, like in Julia, AFAIK. 3. Multiple dispatch seems useful in these cases, although it's a fairly niche area. I don't know how doable would this be just with generic programming / protocols without MD, didn't think too much about it. I vaguely remember that I didn't like that extending code with MD in a way relies on implicit, undefined interfaces.