Skip to content

BigInt doesn't support ++ and -- #50216

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

Closed
JayXon opened this issue Oct 15, 2022 · 7 comments
Closed

BigInt doesn't support ++ and -- #50216

JayXon opened this issue Oct 15, 2022 · 7 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. type-enhancement A request for a change that isn't a bug

Comments

@JayXon
Copy link

JayXon commented Oct 15, 2022

  var i = BigInt.zero;
  print(++i);
Error: A value of type 'int' can't be assigned to a variable of type 'BigInt'.
 - 'BigInt' is from 'dart:core'.
  print(++i);
        ^

bigint in js supports it

@mraleph mraleph added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. type-enhancement A request for a change that isn't a bug labels Oct 17, 2022
@mraleph
Copy link
Member

mraleph commented Oct 17, 2022

/cc @lrhn

@lrhn
Copy link
Member

lrhn commented Oct 17, 2022

That's because BigInt.operator+ does not accept the integer 1 as argument, and both ++v and v++ are defined to perform v = v + 1;.

Nothing much we can do about that without changing the parameter type of BigInt.operator+, which we can't without losing type safety (we'd have to change the parameter type to Object or dynamic, and check that the incoming value is an int or a BigInt).

No current plan to change the behavior.

@lrhn lrhn closed this as completed Oct 17, 2022
@JayXon
Copy link
Author

JayXon commented Oct 17, 2022

I get why this isn't working, but why can't we change ++v to perform v = v + BigInt.one; if v is a BigInt?

@lrhn
Copy link
Member

lrhn commented Oct 17, 2022

We could special-case BigInt, which is something we have so far refrained from doing.

Doing so will be a language change, which requires the special case to be implemented in all our tools. It's not just a quick library change.

I'd loathe to introduce special casing, and then not get anything from it other than helping BigInt with ++.
I'd at least want to help the fixnum numbers as well, and any other number type that people want to cook up, so that we get some more bang for the special-casing buck.

Maybe:

For e++ (etc.) let T be the static type of e. It's a compile-time error of T does not have an operator+.
Let S be the type of the parameter of T.operator+.
If int is assignable to S, let o be the integer 1.
Otherwise if S is a type whose declaration contains a static constant member named one,
whose type is assignable to S, let o bet the value of S.one.
Otherwise, it's a compile-time error.
... more static type checking, like return type of T.operator+ being assignable to declared type of e ...

Then e++ is evaluated as follows:
Evaluate e to a value v.
Invoke operator+ on v with o as argument, let r be the result.
Assign r to e.

I don't think we'll actually do this (probably still too low reward for the effort, there are other higher-priority things to do to the language.)

@JayXon
Copy link
Author

JayXon commented Oct 18, 2022

Thanks, that sounds reasonable, if it's a priority thing, can we leave this issue open?

@lrhn
Copy link
Member

lrhn commented Oct 18, 2022

We can keep it open as a request for allowing user-classes to use ++/-- as well, but then it should be a language issue (in the language repository), so I'll just go file an issue there instead: dart-lang/language#2576

@JayXon
Copy link
Author

JayXon commented Oct 18, 2022

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants