Skip to content
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

Simplify number literals #78

Open
flenniken opened this issue Feb 6, 2016 · 4 comments
Open

Simplify number literals #78

flenniken opened this issue Feb 6, 2016 · 4 comments
Labels

Comments

@flenniken
Copy link

I would like to suggest an enhancement, if it is possible, to simplify how number literals are specified. I would like to specify 3 instead of 3u16, basically for readability.

static MYMAP: phf::Map<u16, &'static str> = phf_map! {
    3u16 => "three",
    12u16 => "twelve",
    2222u16 => "two thousand two hundred twenty two",
};
@sfackler
Copy link
Collaborator

sfackler commented Feb 7, 2016

There isn't a super straightforward way to do this, unfortunately. Syntax extensions run before the compiler knows what types things have, so there's nothing implying that the 15 in

static MYMAP: phf::Map<u16, &'static str> = phf_map! { 15 => "hi" };

is a u16 instead of a u32 or any other type.

One approach could be to treat all numbers as u64 in the internal hashing logic which would avoid that problem.

@flenniken
Copy link
Author

Currently you get an invalid literal error with bare numbers. It took a while for me to figure out that you need to be more specific.

i32 is normally the default if you leave off the suffix. Is there a way to detect bare numbers and use i32?

Using u64 internally sounds like it might work great. So the macro would treat all numbers as u62 then the final compiler pass would use the actual types?

@sfackler
Copy link
Collaborator

sfackler commented Feb 7, 2016

We could absolutely treat unsuffixed integers as i32, but I'd be worried that the behavior would be confusing for people used to those types "actually" being inferred.

In the u64 option, we'd basically cast all numbers up to u64 when hashing them.

@JohnTitor JohnTitor added A-macros Area: phf-macros C-enhancement labels Jun 17, 2021
@baxterjo
Copy link

Don't forget Rust ignores underscores in literal definitions. So 3u16 == 3_u16 just in case that helps for readability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants