One question is the naming: Every operation seems to have a few alternative names, we could evaluate each operation on a case-to-case base or we can decide to pick the same as Java. I checked C#, and they don't seem to provide these operations at all.
Another question is whether smaller types like Byte or Short will automatically gain these methods if we add them to RichInt/RichLong (not sure how the implicit widening conversions interact with the implicit def from <Num> to Rich<Num>), because some methods might exhibit unintuitive semantics here (e. g. reverse will always be zero).
Ideas/objections/opinions?
Thanks and bye,
Simon
/CC'ing Erik
Another question is whether smaller types like Byte or Short will automatically gain these methods if we add them to RichInt/RichLong (not sure how the implicit widening conversions interact with the implicit def from <Num> to Rich<Num>), because some methods might exhibit unintuitive semantics here (e. g. reverse will always be zero).
--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-interna...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Do "these implicit conversions" mean Byte => Int, etc?
It may be before your time, but I tried to do this (i.e. I implemented it and converted the whole codebase) years ago. It was MUCH less of a problem at the user level than I had anticipated going in. In fact, I was invariably glad to get a type error. But this was lumped into the "we have to be like java" blob.
--
// NOTE: no implicit conversionclass RichBitOps32
This way you explicitly state in your code that you're doing an operation on 32 bits. However, you should use short method names inside RichBitOps32 so that the whole thing isn't too clunky.
--
The problem is that it's not clear whether these things are "math" or not. Math is always widened to Int from any smaller numeric type. You can recognize math because it uses symbolic operators. It's annoying, yes, but there's a pretty simple heuristic that you can apply. And really, (x+y).toByte is already hard enough; making you (x.toInt + y.toInt).toByte just obscures the important operation even more in a flurry of type conversions. I can't envision an intermediate scheme which isn't overly burdened with special cases--maybe someone can propose one.
If you use Java, you're either using operators-which-are-math, or you know you are doing something with Int because you're calling java.lang.Integer.foo(x). Whether by accident or design, the current set of methods in Scala are either enriched onto both the smaller types and Int (Paul's warning notwithstanding), or give correct behavior if widened.
So simply allowing the widening with bitCount and friends is a problem because it forces people to add rules, increasing cognitive burden (beyond the burden of remembering that these methods exist at all). It is clunky to have to occasionally reach for java.lang.Integer.bitCount, but at least that keeps things clear.
Thus, I think the sensible options (all with their own drawbacks) are to do one of(1) Do nothing--use java.lang.Integer as your marker for widening.(2) Ignore Paul's don't-rely-on-it advice and write methods for all the small types also
(3) Introduce a much shorter marker than java.lang.Integer like .bits32 or .intOp