-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow user-defined classes to use ++
and --
syntax.
#2576
Comments
A number of fairly outrageous ideas include:
|
@override
Int64 operator +(Object other) {
Int64 o = _promote(other);
int sum0 = _l + o._l;
int sum1 = _m + o._m + (sum0 >> _BITS);
int sum2 = _h + o._h + (sum1 >> _BITS);
return Int64._masked(sum0, sum1, sum2);
} Another alternative could be to rewrite the original implementation with union types (#1222, #83, #145) Int64 operator +(Int64 | int other) {
Int64 o = other is Int64 ? other : Int64(other);
int sum0 = _l + o._l;
int sum1 = _m + o._m + (sum0 >> _BITS);
int sum2 = _h + o._h + (sum1 >> _BITS);
return Int64._masked(sum0, sum1, sum2);
} |
How about this: void operator ++();
void operator --(); That way /// Assuming a lot about how BigInt works,
class BigInt {
void operator ++() { this.innerValue++; }
void operator --() { this.innerValue--; }
} |
Using I think I'd rather have Then we can introduce (C# special-cases |
A number class like
BigInt
cannot use the increment/decrement operators.An example like
BigInt x = BigInt.zero; while (something) ++x;
fails complaining thatint
is not assignable toBigInt
.The reason for that is that
++x
is defined to be equivalent tox = x + 1
, and theBigInt.operator+
operator does not acceptint
as argument.A similar class, from
package:fixnum
, isInt64
. It does work with++
because itsoperator+
accepts multiple argument types, includingint
, by having an argument type ofObject
.That's bad for typing, but convenient if you want to mix integers and fixnum-types.
BigInt
chose being well-typed over that convenience. In most cases that's OK, you can writex = x + BigInt.one
orx += BigInt.one
, but you don't get the convenience of++
/--
.The text was updated successfully, but these errors were encountered: