ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "znz (Kazuhiro NISHIYAMA)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:105146] [Ruby master Misc#18150] Proposal: Deprecate leading zero syntax to declare octals, since it's extremely confusing (and Python 3 removed it too)
Date: Sat, 04 Sep 2021 03:21:51 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-93553.20210904032149.12446@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-18150.20210903171005.12446@ruby-lang.org

Issue #18150 has been updated by znz (Kazuhiro NISHIYAMA).


I think  supporting `08` and `09` only satisfy such usecases.

----------------------------------------
Misc #18150: Proposal: Deprecate leading zero syntax to declare octals, since it's extremely confusing (and Python 3 removed it too)
https://bugs.ruby-lang.org/issues/18150#change-93553

* Author: ProGM (Piero Dotti)
* Status: Open
* Priority: Normal
----------------------------------------
Hi there,
I'd like to open a discussion about the leading zeros syntax to declare octal numbers.

Let me give you a little bit of context.

It seems like ruby considers all integers with leading zeros as octal.
For instance, if you write 012, ruby reads it as 10 in decimal.
There is an alternative syntax for this, using an "o" character after the zero, i.e. 0o12


I've discovered this behavior by chance a couple of days ago.
I was declaring a new Date object:

``` ruby
START_DATE = Date.new(2021, 09, 01)
```

In my mind, I was thinking of ISO 8601 ("2021-09-01") and I wrote it in that way without even thinking about it.

I immediately got a weird error:
``` ruby
SyntaxError ((irb):2: Invalid octal digit)
```
That was astonishing. "What the heck does this error mean?", I thought.


After a brief research, I've discovered that many languages that come from C have this "weird" behavior.
Javascript, Go, Java, and ruby itself all treat leading zeros like this.
Rust and Elixir, instead, are parsing 00123 like 123, as math notation suggests.
Finally, Python 3+ raises an error.

---

My proposal is: throw a deprecation warning in ruby 3.x when using this syntax and treat 0011 as 11 from ruby 4+.

Why?
* Because it's extremely confusing
* There is an alternative syntax that is much more explicit (0o11 vs 011)
* It leads to weird behaviors (like 011 != 11)
* Because Python 2.x was treating leading zeros numbers as octals like ruby does, but this has been changed in python 3+: https://medium.com/techmacademy/leading-zeros-in-python-a-hidden-mystery-revealed-ee3e3065634d


Ruby's claim is "A PROGRAMMER'S BEST FRIEND". As a programmer, I don't consider this behavior very friendly. :(

Let me know what you think about this!




-- 
https://bugs.ruby-lang.org/

      parent reply	other threads:[~2021-09-04  3:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-03 17:10 [ruby-core:105142] [Ruby master Misc#18150] Proposal: Deprecate leading zero syntax to declare octals, since it's extremely confusing (and Python 3 removed it too) ProGM (Piero Dotti)
2021-09-03 19:56 ` [ruby-core:105144] " mame (Yusuke Endoh)
2021-09-04  3:21 ` znz (Kazuhiro NISHIYAMA) [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-93553.20210904032149.12446@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).