elixir icon indicating copy to clipboard operation
elixir copied to clipboard

Invalid octal number 080 does not cause error

Open niku opened this issue 12 years ago • 10 comments

/Users/niku% iex
Erlang/OTP 17 [erts-6.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Interactive Elixir (0.14.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> 070
56
iex(2)> 080
80

I expect 080 cause error. Because 080 is not valid octal number.

niku avatar Jul 29 '14 14:07 niku

Oh dear.

Instead of even dealing with this issue, can we please fix the language?

A prefix of 0 meaning octal appears to be a C idiosyncrasy dating back to the 70s, if not earlier. Very few new programming languages go with this odd, inconsistent, and frankly bug-prone lexical convention. It's reasonably common for decimal numbers to be prefixed with zero(es) which can lead to all sorts of fun bugs because of compilers thinking that the programmer is trying to write an octal number. In addition, it completely defies logic to use 0b for binary numbers, 0x for hexadecimal numbers, and then 0 for octal numbers in the same language.

I really think Elixir needs to go with 0o as all recent languages I can think of do (F#, Rust, Nimrod, Swift, ...). D even did away with it in the language and moved it into a compile-time template...

alexrp avatar Jul 29 '14 16:07 alexrp

@alexrp you are right. Let's fix this.

josevalim avatar Jul 29 '14 16:07 josevalim

What should we do for strings? "\o123" to mirror "\x123"?

josevalim avatar Jul 29 '14 16:07 josevalim

@josevalim IMO yes.

alexrp avatar Jul 29 '14 16:07 alexrp

The only inconsistency here is that we don't support "\b111" inside strings. /cc @ericmj @alco

josevalim avatar Jul 29 '14 16:07 josevalim

Personally, I question the value of even being able to insert arbitrary data into what is supposed to be a UTF-8 string. Being able to say "\U+NNNN..." (or whatever) to indicate a code point should be safe and verifiable at compile time and (at least in my experience) should cover all use cases.

(But that's of course unrelated to the main issue here.)

alexrp avatar Jul 29 '14 16:07 alexrp

Final question: should we keep \0 inside strings for the null byte?

José Valimwww.plataformatec.com.br http://www.plataformatec.com.br/Founder and Lead Developer

josevalim avatar Jul 29 '14 16:07 josevalim

I like the idea of using 0o123 instead of 0123. It makes the code more clear also. But what about the upper case o. o is really simillar to 0. even equals visually depending on the font you are using. the code 0O123 would be a little weird. Course, I'm assuming that the rule would apply the same for X and B for Hex and B. I don't known if it is implemented that way on other languages also.

pedrosnk avatar Jul 29 '14 16:07 pedrosnk

At least Nimrod does not allow an uppercase O for exactly that reason, and I think that's a good thing. If it was up to me, lowercase letters would also be disallowed for other base indicators (binary, hexadecimal) for clarity and consistency... but I get that this may be controversial.

alexrp avatar Jul 29 '14 16:07 alexrp

Added 0o for numbers, deprecated the leading 0 in both strings and for regular numbers.

josevalim avatar Jul 29 '14 18:07 josevalim