4 ms·
Do you mean something like "compound assignment operators"? Apparently you can define custom operators like: func += (inout left: Vector2D, right: Vector2D
by eliteraspberrie 11y ago
Do you mean something like "compound assignment operators"? Apparently you can define custom operators like:
func += (inout left: Vector2D, right: Vector2D) {
left = left + right
}
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-ID28 https://developer.apple.com/library/ios/documentation/Swift/...
- dbpokorny 11y agoQuestion of clarification: is this the same concept as Python's __iadd__? I was not aware that this was a "thing" in programming language design. http://stackoverflow.com/questions/1047021/overriding-in-python-iadd-method http://stackoverflow.com/questions/1047021/overriding-in-pyt...
- Veedrac 11y agoLooking at eliteraspberrie's example, not really. Python's __iadd__ and such are not able to reassign. There is no "inout" in Python. The assignment aspect of __iadd__ is done through self mutation (and an extra hard-coded assignment to support immutable variables).
- ben-schaaf 11y agoThe assignment aspect of __iadd__ is done like any other assignment in python. The one to one equivalent of eliteraspberrie's would be: class Vector2D: def __iadd__(self, other) return self + other No "self mutation" as you suggest. a += b is equivalent to a = a.__iadd__(b)
- Veedrac 11y ago> No "self mutation" as you suggest. Your example would be the implementation for immutable Vector2Ds, and is implemented for you if you just wrote __add__. (I presume object.__iadd__ falls back to self.__add__.) The only time you should actually override __iadd__ is for mutable objects, in which case it should look more like class Vector2D: def __iadd__(self, other) for i, x in enumerate(other): self[i] += x return self That's the self mutation I was talking about. Well, strictly that's not OK either, since operators shouldn't be duck-typed. Instead you should do class Vector2D: def __iadd__(self, other) if not isinstance(other, Vector2D): return NotImplemented for i, x in enumerate(other): self[i] += x return self
- dbpokorny 11y ago> The only time you should actually override __iadd__ is for mutable objects What would the rationale be for such a rule? the docs don't mention anything like that https://docs.python.org/2/library/operator.html https://docs.python.org/2/library/operator.html and I've never heard it before. In Python, neither integers nor strings are mutable and both support the "x += y" syntax. The __iadd__ method works the same for both mutable and immutable objects AFAIK
- Veedrac 11y ago> What would the rationale be for such a rule? Because __iadd__ automatically delegates to __add__. For immutable objects that's the best you can do, so there's no reason to write both.