Underscores in Apple’s Swift Numbers

I’m catching up with Apple’s new language Swift, and this declaration of numbers caught my attention: in order to help readability numerals can be written with underscores (eg: 12_345).

Apple Swift Logo

Numeric literals can contain extra formatting to make them easier to read. Both integers and floats can be padded with extra zeroes and can contain underscores to help with readability. Neither type of formatting affects the underlying value of the literal:

let paddedDouble = 000123.456 let oneMillion = 1_000_000 let justOverOneMillion = 1_000_000.000_000_1

Excerpt From: The Swift Programming Language.”.

I also just learned that this feature is already present in some languages such as Perl, Ruby and Java but not in Javascript :(

I like the idea of not only creating new languages, but rethinking how we write code and how we can improve the coder experience (after UX, we now need a new field of CX).

June 10, 2014 ☼ code