-
Notifications
You must be signed in to change notification settings - Fork 23
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
User defined integer literals #216
Comments
@Araq and I discussed this too and came to the same idea as presented in this RFC. |
That does work though with current Nim. template `-`(x: SomeUnsignedInt): typeof(x) = x
echo -3'u8 |
@hlaaftana I'm talking about the fact that 128 is not a uint8 but -128 is. |
oh i wasn't aware of this when I wrote #228 |
It is my intent to have PR for this placed before the end of February 2021. Perhaps sooner as I already have it working; I just need to write more test cases to confirm it handles more border conditions. As to the case-sensitivity, I plan on maintaining that. If you want both a My motive is to allow this function to enable the IEEE 745 decimal library (#308) I'm writing to also use this. Specifically "m" will be the suffix (a convention used in C# and a few other languages.) One of my goals to have this fully convert/resolve at compile-time. var a = 1234.56E7m
# equivalent to:
var otherA = "1234.56E7".m The I will be also make updates to documentation. |
Will be solved by nim-lang/Nim#17020, though it looks like we should probably do without optional |
nim-lang/Nim#17489 will soon be merged. This RFC has been implemented. |
Lexer-level new language feature and very low priority (like Nim 2.0-not this year kind of low), but it comes too natural to not write down.
Abstract
Nim currently accepts the syntax
123'i32
and123i32
as a syntax-level version ofint32(123)
, mapping to the AST kindnkInt32Lit
(I'm going to be giving ints as examples for this RFC but floats also work for all of it). This RFC, on top of this syntax existing, generalizes this and, similar to raw string literals, makes123'name
call a routinename(123)
where123
is ofnkIntLit
, or, piggybacking off another RFC/feature request that I can't find, some kind ofnkRawIntLit
(vsnkRawFloatLit
)/nkRawNumberLit
which contains a string of the integer.123name
might also work and, unless the lexer is smart, ifname
doesn't start withe
ori
/d
/f
. Alsoname
can't be one ofi i8 i16 i32 i64 u u8 u16 u32 u64 f d f32 f64
Motivation
Arbitrary/high precision integer/decimal/float libraries will benefit from a general number literal that uses a string (though I think this was part of the other RFC that I'm imagining). Currently what they have to do is something like
xl"245"
(though none I've seen take advantage of this) vs245'xl
. This also makes sure that the string isn't a deformed number at lexing time.Also works well for DSLs like
12kg
/12'kg
, but this is already doable with12.kg
, with the cost of being less readable/harder to parse (.
could be a decimal point).Description
This is essentially the user defined literals from C++, except instead of starting the identifier with an underscore it starts it with a single quote, in consistency with Nim's previous
1'i32
syntax. Nim also already has user defined raw string literals, but as prefixes instead of postfixes, consistent with Python instead of C++.This getting implemented would make it possible for number literals to have side effects, though this is already possible for raw string calls like
foo"abcd"
.Another issue is case sensitivity: Currently
1'I32
is the same as1'i32
, but routines in Nim are case sensitive for the first letter. This means the existing number tags (i32 etc) have to get special case insensitive treatment but normal identifiers don't, causing an inconsistency.Yet another issue is bases; octal, binary, hexadecimal literals. These would be stored verbatim in string form, and any parser that gets called by number literals will have to account for them.
Examples
Before
After
Backward incompatibility
A lot. A new
nkRawIntLit
would have to be at the end of the node kind enum to keep binary compatibility, but this would keep it outside of all the other number literal/int literal/literal ranges. Ditto for ranges of what hasstrVal
. This could be fixed by makingnkIntLit
and similarlynkFloatLit
store strings instead of parsed numbers, but this is still binary incompatible.Syntax highlighting tools would suffer, though it wouldn't be module or line breaking bad like #161, it would be a simple token-local mishap. A lot of highlighting tools don't even recognize
1'i32
though.As mentioned previously, this would mean number literals can have side effects.
Separately from this post, I think
1.4d
syntax should be deprecated, it ruins possibilities for a whole letter and Nim doesn't call them "doubles" except to be compatible with C. Might need another RFC for that though and I don't want to botherThe text was updated successfully, but these errors were encountered: