-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Numeric literal semantics #144
Numeric literal semantics #144
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have more general thoughts, but I need to gather them and figure out exactly what to say. I'll probably do some discussing on Discord first and then post more comments here. Here are my thoughts so far:
My general direction is that I would want to consider fitting integer literals into a broader plan for integers in general. Some of the decisions we can make with how to handle integers makes some of these choices a bit more "obvious".
I have written a C++ integer library: https://bitbucket.org/davidstone/bounded_integer/ where the general idea is that users specify min and max values that the integer type can use (bounded::integer<0, 10>
holds values between 0 and 10 inclusive). If we decide to go in that direction for integers, the "obvious" answer (but not the only answer) to integer literals is that they would be of type Integer<N, N>
(for which my library provides an alias template bounded::constant_t<N>
for types and bounded::constant<N>
for values). These are unit types with no storage, much as outlined in this proposal.
We triage inactive PRs and issues in order to make it easier to find active work. If this PR should remain active, please comment or remove the |
We triage inactive PRs and issues in order to make it easier to find active work. If this PR should remain active or becomes active again, please reopen it. |
represent all rationals, not only dyadic and decadic rationals. For example, we need to be able to represent 1.0 / 3.0 exactly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm generally happy with this direction. Mostly waiting for folks who've commented to have a chance to resolve comments based on updates to the document.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple more comments
(note, I think the -
comment from before may be obsolete under the current doc)
…of a Carbon.Rational. Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe all review comments have been addressed.
adding an AnyIntLiteral interface, clarify that two listed disadvantages are covering two different possibilities.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
This proposal provides semantics for numeric literals. * Numeric literals have a type derived from their value, and can be converted to any type that can represent that value. * Simple operations such as arithmetic that involve only literals also produce values of literal types. * Literals implicitly convert to types that can represent them. * The Carbon prelude provides: * An arbitrary-precision integer type `BigInt`. * A rational number type `Rational(T:! Type)` with constraints on `T` not yet determined. * A family of integer literal types, `IntLiteral(N:! BigInt)`. * A family of real literal types, `RealLiteral(N:! Rational(BigInt))`. Co-authored-by: Chandler Carruth <chandlerc@gmail.com> Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
This PR aims to complete issue #1997.
This proposal provides semantics for numeric literals.
Numeric literals have a type derived from their value, and can be converted to any type that can represent that value.
Simple operations such as arithmetic that involve only literals also produce values of literal types.
Literals implicitly convert to types that can represent them.
The Carbon prelude provides:
BigInt
.Rational(T:! Type)
with constraints onT
not yet determined.IntLiteral(N:! BigInt)
.RealLiteral(N:! Rational(BigInt))
.Examples:
42
has typeIntLiteral(42 as BigInt)
.5.2
has typeRealLiteral(5.2 as Rational(BigInt))
.