News for hackers. Stuff that matters.
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?
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.

[0] https://en.wikipedia.org/wiki/Ring_(mathematics)

[1] https://en.wikipedia.org/wiki/Field_(mathematics)

Re:Not Growing a Language by klyrs on Saturday April 03, 2021 @03:48PM (#26684127)
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?
Re:Not Growing a Language by dllthomas on Saturday April 03, 2021 @08:08PM (#26685866)
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).
Re:Not Growing a Language by phoe-krk on Saturday April 03, 2021 @02:49PM (#26683612)
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.

Re:Not Growing a Language by zokier on Saturday April 03, 2021 @04:05PM (#26684277)
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.
Re:Not Growing a Language by hinkley on Saturday April 03, 2021 @06:21PM (#26685270)
There is a level of stupid that is deniable, and a level that is not. MyString extends Number is going to lead more swiftly to words with your coworkers.