Overloading
Overloading
Posted Apr 26, 2014 10:54 UTC (Sat) by rsidd (subscriber, #2582)In reply to: Overloading by tialaramex
Parent article: Porting the Go compiler to Go
Not C++ but python, and libraries (numpy, sympy) that others wrote and I use (and, rarely, read and try to understand). It makes complete sense to allow arithmetic operations (+, *) to matrices and symbolic types, and python lets you define methods in the class definitions that let you do that. Of course it make no sense to define "+" to mean something totally different, like exponentiation. The language provides tools, but can't force common sense on the programmer. Properly done, operator overloading makes such code (ie code using user-defined or library-defined mathematical objects) more readable, not less.
The other extreme is OCaml which has different operators for integer and floating-point arithmetic. Now that makes code harder to read, or write.
Posted Apr 26, 2014 13:57 UTC (Sat)
by aggelos (subscriber, #41752)
[Link] (1 responses)
Posted Apr 26, 2014 14:34 UTC (Sat)
by rsidd (subscriber, #2582)
[Link]
Posted May 19, 2014 7:38 UTC (Mon)
by marcH (subscriber, #57642)
[Link] (2 responses)
"1.0 +. 2.0" is very hard to read, really?
The problem with operator overloading (and many other C++ features, or Perl,...) is giving kids matches. You CANNOT rely on programmers not to abuse them, it just does NOT work in real life. Sure, you can always find some teams of geniuses so good they don't live in real life (lucky you if you are part of one) but you also need programming languages for the masses.
Ocaml's "+." is the perfect balance. It's just as readable as a plain "+" without falling into the operator overloading trap.
Posted May 19, 2014 14:11 UTC (Mon)
by mathstuf (subscriber, #69389)
[Link] (1 responses)
Posted May 19, 2014 17:06 UTC (Mon)
by marcH (subscriber, #57642)
[Link]
Also, this discussion should probably make a clearer difference between built-in versus user defined operators.
Overloading
The other extreme is OCaml which has different operators for integer and floating-point arithmetic. Now that makes code harder to read, or write.
Actually, I've found that to be quite helpful when writing code and I don't think +. (floating point addition operator in OCaml) is any harder to read than + (integer addition). Same for lack of automatic integer type promotions (wish I could opt out of that in some C code). I don't doubt there's programs where the extra type conversion calls would get in the way, but for most of the programs I write these days, it's nice when the type system discourages sloppy thinking about arithmetic. YMMV :-)
Overloading
Overloading
Overloading
Overloading
