Is there interest in a SIP cleaning up/improving numeric literals?

334 views
Skip to first unread message

Simon Ochsenreither

unread,
Nov 11, 2011, 8:36:29 AM11/11/11
to scala-...@googlegroups.com
After code like “1.” or “42.d” is finally deprecated I wonder if it makes sense and clean up literals, once and for all?

I have mostly three things in mind:

1. Octal literals

I have never seen them anywhere, neither in Java nor in Scala, yet they can be confusing and surprising for beginners, e. g. 012 == 10.

Suggestion: Remove them completely.
Alternative: Use a safer prefix, like 0o. This would be in line with the hexadecimal prefix 0x.

2. Binary literals

Binary literals would be nice and certainly much more useful then octals.
This addition is backward compatible. All existing source code would stay valid.

Proposed syntax: 0b, like the hexadecimal prefix and the same as in Java 7.
Alternative: Don't add them.

3. Seperators in literals

Pretty much comparable to “Underscores in literals” in Java 7.
The idea is to allow optional separators in literals to make them more readable.
This addition is backward compatible. All existing source code would stay valid.

Proposed syntax: Use ' instead of _.
E. g.
  val twoMillions = 2'000'000
  val classfileMagic = 0xCAFE'BABE
  val someFlags = 0b00'111'11 // Bits 1 and 2: .... Bits 3 to 5: .... Bits 6 and 7: ....
Alternative: Don't add them.

What do you think?

Thanks and bye,


Simon

Tom Switzer

unread,
Nov 11, 2011, 8:50:00 AM11/11/11
to scala-...@googlegroups.com

Separators would be great addition. Our brains (mine at least) just aren't made to comprehend 0x0007F80800000000L at a glance. It leads to double and triple checking trivial things and bugs when we don't.

Tom Switzer

unread,
Nov 11, 2011, 8:54:46 AM11/11/11
to scala-...@googlegroups.com

Plus, as it is, I already add separators when I write the code (or am debugging it), then remove them after I finish writing/checking the number so it'll compile. I'm guessing a lot of other folks do this too.

Seth Tisue

unread,
Nov 11, 2011, 8:59:09 AM11/11/11
to scala-...@googlegroups.com
I don't have a strong opinion about the other two changes, but I would love to see octal literals deprecated. Octal literals are horse-and-buggy stuff.

-- 
Seth Tisue | Northwestern University | http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/

Paul Phillips

unread,
Nov 11, 2011, 9:07:41 AM11/11/11
to scala-...@googlegroups.com
Oh, this thread broke out while I was writing the email to
scala-language. So octals are being assaulted on two fronts.

My experience is that martin will nix any new literals in the core
language. They'd be very easy to graft on though.

Rex Kerr

unread,
Nov 11, 2011, 10:32:32 AM11/11/11
to scala-...@googlegroups.com
On Fri, Nov 11, 2011 at 8:36 AM, Simon Ochsenreither <simon.och...@googlemail.com> wrote:
After code like “1.” or “42.d” is finally deprecated

This would be nice, but is independent of the other suggestions.
 
1. Octal literals

I have never seen them anywhere, neither in Java nor in Scala, yet they can be confusing and surprising for beginners, e. g. 012 == 10.

Suggestion: Remove them completely.
Alternative: Use a safer prefix, like 0o. This would be in line with the hexadecimal prefix 0x.

I prefer the alternative, but either way is fine as long as 012 goes away as fast as possible.
 
2. Binary literals

Binary literals would be nice and certainly much more useful then octals.
This addition is backward compatible. All existing source code would stay valid.

Proposed syntax: 0b, like the hexadecimal prefix and the same as in Java 7.
Alternative: Don't add them.

In the long run, this is a good idea for compatibility with Java code that does bit-twiddling.
 
3. Seperators in literals

Pretty much comparable to “Underscores in literals” in Java 7.
The idea is to allow optional separators in literals to make them more readable.
This addition is backward compatible. All existing source code would stay valid.

Proposed syntax: Use ' instead of _.
E. g.
  val twoMillions = 2'000'000
  val classfileMagic = 0xCAFE'BABE
  val someFlags = 0b00'111'11 // Bits 1 and 2: .... Bits 3 to 5: .... Bits 6 and 7: ....
Alternative: Don't add them.

I think this is the job of editors, not the source code.  It's just syntax highlighting--should we also be able to surround keywords like if with # so they stand out better?  I don't like the change in Java and I like this even less.  I certainly don't want ', since it's just going to confuse every existing simpleminded syntax highlighting scheme _and_ doesn't match Java code.  Either Scala should adopt exactly the Java syntax (making numeric code more trivially portable), or Scala should do nothing.

  --Rex

Erik Osheim

unread,
Nov 11, 2011, 10:37:17 AM11/11/11
to scala-...@googlegroups.com
On Fri, Nov 11, 2011 at 05:36:29AM -0800, Simon Ochsenreither wrote:
> Suggestion: Remove them completely.
> Alternative: Use a safer prefix, like 0o. This would be in line with the
> hexadecimal prefix 0x.

I like using a 0o prefix. That said, I wouldn't cry if these were removed.



> Proposed syntax: 0b, like the hexadecimal prefix and the same as in Java 7.
> Alternative: Don't add them.

Python has these, I think they're nice.



> Proposed syntax: Use ' instead of _.
> E. g.
> val twoMillions = 2'000'000
> val classfileMagic = 0xCAFE'BABE
> val someFlags = 0b00'111'11 // Bits 1 and 2: .... Bits 3 to 5: .... Bits
> 6 and 7: ....
> Alternative: Don't add them.

Separators would be great! I do the thing Tom mentions where I type
separators in myself and then have to delete them later.

I actually like underscores better but would be fine with ' too. I
think this proposal should have two alternatives: use _ or don't add
them, since many people might not like 3'000 but would like 3_000.

-- Erik

Kieron Wilkinson

unread,
Nov 11, 2011, 12:14:36 PM11/11/11
to scala-...@googlegroups.com
On Friday, 11 November 2011 13:36:29 UTC, Simon Ochsenreither wrote:
1. Octal literals

+1 The current syntax has bitten me too.
 
2. Binary literals

+100 It's great that Java finally added there. I like mucking about with low-level stuff, and it would be really nice to say what you mean when bit-twiddling. It's great Java finally got this, but since I don't use Java in side projects now, it's not so helpful for me. If performance is not a concern, I sometimes even use strings of bits to make the code clearer (yeah, I know, but use also can use seperators that way).

3. Seperators in literals

Absolutely. If you are implementing binary literals, this basically goes hand-in-hand with that. Doing binary literals would be pretty small win without this (obviously even more important for values bigger than bytes). It would also have to be arbitary placement too. Sometimes I might want to say 001_000_11 sometimes 0001_0001, etc. depending on what the bits meant, since they might be logically grouped by a binary protocol, and it would be good to make that grouping show in the code without needing a comment to spell it out. Don't care much about syntax - the underscore is already pretty overloaded, but it would be nice to stay the same as Java (and I can't think of anything better).

Kieron

Daniel Sobral

unread,
Nov 11, 2011, 2:01:23 PM11/11/11
to scala-...@googlegroups.com
Yes, yes and yes, in that order.

--
Daniel C. Sobral

I travel to the future all the time.

Peter C. Chapin

unread,
Nov 11, 2011, 2:02:38 PM11/11/11
to scala-...@googlegroups.com

I like the idea of separators in literals also. The underscore seems natural to me but I understand Scala already makes heavy use of it in other contexts. Still, compatibility with Java is good in something like this (one less surprise when moving from Java to Scala). Also there are other languages that use the underscore in literals also (Ada). Using ‘ as a separator does not appeal to me at all. For one thing it would make numeric literals look too much like some kind of character literal. Quick, what’s the difference here:

 

val x = ‘0’

valy = 0’1’0

 

Peter

Daniel Sobral

unread,
Nov 11, 2011, 2:02:55 PM11/11/11
to scala-...@googlegroups.com
Only I wouldn't use ' as separator. _ is in line with what is being
proposed (has been adopted?) for Java, and ' will give trouble with
syntax highlighting. Again.

On Fri, Nov 11, 2011 at 11:36, Simon Ochsenreither
<simon.och...@googlemail.com> wrote:

--

Russ Paielli

unread,
Nov 11, 2011, 3:19:37 PM11/11/11
to scala-...@googlegroups.com
Floating-point numbers can be expressed in scientific notation, as in

val x = 3.0e8

Having a way to express large integers the same way would nice. Ideally, the same notation would be used, and the compiler would distinguish between integers and floating-point numbers the same way it does without the exponent. So, for example, 3.0e8 would be a Double, and 3e8 would be an integer. However, that will break backwards compatibility, so we need a different approach. How about something like

val x = 3z10 // 3 followed by 10 zeros

--Russ P.
--
http://RussP.us

Erik Osheim

unread,
Nov 11, 2011, 3:30:54 PM11/11/11
to scala-...@googlegroups.com
On Fri, Nov 11, 2011 at 12:19:37PM -0800, Russ Paielli wrote:
> val x = 3z10 // 3 followed by 10 zeros

This is a neat idea but I doubt it would make it into the library, and
I don't think defining your own version is that hard:

----begin snip----

scala> implicit def longNotation(n:Long) = new { def z(i:Int) = n * scala.math.pow(10, i).toLong }
longNotation: (n: Long)java.lang.Object{def z(i: Int): Long}

scala> 4.z(3)
res1: Long = 4000

scala> 4.z(10)
res2: Long = 40000000000

----end snip----

In this case I don't think it's a big enough advantage to build this
into the language spec (versus just using an implicit like the one
above).

-- Erik

Stefan Wagner

unread,
Nov 12, 2011, 3:04:21 AM11/12/11
to scala-...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 11.11.2011 14:54, schrieb Tom Switzer:
> Plus, as it is, I already add separators when I write the code (or am
> debugging it), then remove them after I finish writing/checking the number
> so it'll compile. I'm guessing a lot of other folks do this too.

My workaround so far is, for high decimal numbers:

val primes = createPrimes (17*1000*1000)

Of course
val x = (13*1000*1000) + (47*1000) + 139
is not that readable, as
val x = 13'047'139

Wha not use comma as delimiter?
val x = 13,047,139

For hexcode, isn't the colon a more convenient delimiter?
val k = 0x43c4:ce10:88d3
val bin=0b0110:0011:1010:1001

Without thinking about possible pitfalls, or knowing what is best to
implement for a tokenizer/parser...

- --

Tsch���--->...Stefan
- ---------------------------
Don't visit my homepage at:
http://home.arcor-online.net/hirnstrom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk6+KIUACgkQQeATqGpDnRoTFgCfTW1VCy6OdoQMcarNgCf5xrNn
IPwAn1Ro18NkEj/hMU+IozpSD5Fam0t/
=O5K4
-----END PGP SIGNATURE-----

Paul Hudson

unread,
Nov 12, 2011, 3:52:24 AM11/12/11
to Stefan Wagner, scala-...@googlegroups.com
>Why not use comma as a delimiter?

val x = f(13,047,139)

On 12/11/2011, Stefan Wagner <hirn...@arcor.de> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Am 11.11.2011 14:54, schrieb Tom Switzer:
>> Plus, as it is, I already add separators when I write the code (or am
>> debugging it), then remove them after I finish writing/checking the number
>> so it'll compile. I'm guessing a lot of other folks do this too.
>
> My workaround so far is, for high decimal numbers:
>
> val primes = createPrimes (17*1000*1000)
>
> Of course
> val x = (13*1000*1000) + (47*1000) + 139
> is not that readable, as
> val x = 13'047'139
>
> Wha not use comma as delimiter?
> val x = 13,047,139
>
> For hexcode, isn't the colon a more convenient delimiter?
> val k = 0x43c4:ce10:88d3
> val bin=0b0110:0011:1010:1001
>
> Without thinking about possible pitfalls, or knowing what is best to
> implement for a tokenizer/parser...
>
> - --
>

> Tschööö--->...Stefan


> - ---------------------------
> Don't visit my homepage at:
> http://home.arcor-online.net/hirnstrom
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk6+KIUACgkQQeATqGpDnRoTFgCfTW1VCy6OdoQMcarNgCf5xrNn
> IPwAn1Ro18NkEj/hMU+IozpSD5Fam0t/
> =O5K4
> -----END PGP SIGNATURE-----
>

--
Sent from my mobile device

Eugen Labun

unread,
Nov 16, 2011, 3:55:00 AM11/16/11
to scala-...@googlegroups.com, Simon Ochsenreither
On 2011-11-11 14:36, Simon Ochsenreither wrote:
> After code like “1.” or “42.d” is finally deprecated

Sorry for ignorance, where is it deprecated?

Adriaan Moors

unread,
Nov 16, 2011, 6:09:12 AM11/16/11
to Eugen Labun, scala-debate

Simon Ochsenreither

unread,
Nov 16, 2011, 6:35:56 AM11/16/11
to scala-...@googlegroups.com, Eugen Labun, adriaa...@epfl.ch
Hi,

I downloaded a recent version, and I'm a bit confused, because 1.+(2) doesn't compile at all anymore.
Wasn't the idea of deprecating literals like 1. that it's easily understandable what the dot does?

1.+(2) complains about the illegal literal instead of being the equivalent of 1 + 2.

What's happening here?

Thanks and bye,

Simon

Adriaan Moors

unread,
Nov 16, 2011, 7:36:04 AM11/16/11
to scala-...@googlegroups.com
i'm on r26010 and can't repro:

Welcome to Scala version 2.10.0.r26010-b20111115113131 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).
Type in expressions to have them evaluated.
Type :help for more information.

scala> 1.+(2)
warning: there were 1 deprecation warnings; re-run with -deprecation for details
res0: Double = 3.0

could you be more specific about which revision you're using, what the program is that you're trying to compile, which error message is printed exactly, ...

Eugen Labun

unread,
Nov 16, 2011, 8:07:34 AM11/16/11
to scala-...@googlegroups.com
> scala> 1.+(2)
> warning: there were 1 deprecation warnings; re-run with -deprecation for details
> res0: Double = 3.0

This is Scala 2.10. And in 2.11 it will be an Int 3 without warnings. Correct?

Simon Ochsenreither

unread,
Nov 16, 2011, 9:21:57 AM11/16/11
to scala-...@googlegroups.com
That's what I assumed, but -Xfuture doesn't compile at all.

Simon Ochsenreither

unread,
Nov 16, 2011, 9:22:38 AM11/16/11
to scala-...@googlegroups.com, adriaa...@epfl.ch
Sorry, forgot to tell that I ran with -Xfuture. Without it is just a warning, but the problem question remains...
Reply all
Reply to author
Forward
0 new messages