Re:Not Growing a Language
by zokier on Saturday April 03, 2021 @02:25PM
(#26683405)
What would be the definition of such "Number" class? How would it prevent any abuse?
[ Parent
| on HN ]
Re:Not Growing a Language
by 6gvONxR4sf7o on Saturday April 03, 2021 @02:58PM
(#26683700)
A ring [0] might be too general, but I can't think of anything that isn't a ring that I'd want to call a "number" class. A field [1] is less general and might be a better fit. It probably depends whether you want a matrix to be able to be a "number," which I'd be inclined to do.
The type systems underlying computer algebra systems like Sage, GAP, etc. are immensely more complex than this. Read the source of Sage (lots of python/cython) and the source of GAP (pure c; everything is basically a void *). Operator overloading is immensely helpful for entire classes of objects -- groups, rings, fields, categories, combinatorial objects... some of them are kinda number-like, but for example groups can support multiplication and division but not addition/subtraction (unless it's an abelian group and you'd rather represent it that way). I've been writing C for nearly 30 years, and GAP is a slog to read. Sage makes this stuff way easier.
In short... math isn't just arithmetic, isn't even arithmetic of complex tensors. Math is way bigger than most programmers seem to comprehend. I'm on the other side of the DK curve; I know that I've only scratched the surface despite a decade of dedicated study
Re:Not Growing a Language
by red_trumpet on Saturday April 03, 2021 @03:18PM
(#26683883)
Natural numbers don't form a ring, and integers don't form a field.
Re:Not Growing a Language
by 6gvONxR4sf7o on Saturday April 03, 2021 @03:50PM
(#26684140)
Good callout. What would you use if you had to restrict operator overloading to an appropriate “number” abstraction?
If we're actually requiring things be a ring, rather than approximating something that is a ring, that rules out floating point numbers (lack of associativity, at least).
As for definition, it already exists: https://docs.oracle.com/javase/8/docs/api/java/lang/Number.h...
As for abuse prevention: it would be impossible to use the common operators like +, -, +=, <<, >> and so on for anything that is not numbers, and therefore prevent C++ levels of overloading abuse.
But any class can inherit that Number, there is nothing enforcing that the subclass is particularly number-like. You can do just stuff like
class MyString extends Number
and you get access to operator overloads. Only thing that makes this annoying is Javas lack of multiple inheritance, but that is pretty minor thing.