4 ms·
In a statically-typed language you’d just get a compile error. The compiler knows the types of the values in the map and whether or not the (+) operation is va
by mason55 7y ago
In a statically-typed language you’d just get a compile error. The compiler knows the types of the values in the map and whether or not the (+) operation is valid for them. You’d likely need a little more code than what’s in the GP post, something restricting the values in your map to types that implement “addable” or whatever the terminology is for your language.
In a dynamically-typed language you’d get some kind of runtime error.
Just think of (+) like any other function and what would happen in your language if you call a function with types that the function doesn’t support.
- AzzieElbab 7y agoWell, in Scala with default predef it would depend on the order or the arguments. If first one is a string it would just append string representation of the second one. One could use a random generator to guess what would happen in JavaScript. I do not know what Python does in these cases
- AzzieElbab 7y agoSorry for being cryptic, the point was that I prefer appenders being explicitly documented in types or method signatures instead of some built-in magic