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.
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 experience is that martin will nix any new literals in the core
language. They'd be very easy to graft on though.
After code like “1.” or “42.d” is finally deprecated
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.
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
1. Octal literals
2. Binary literals
3. Seperators in literals
--
Daniel C. Sobral
I travel to the future all the time.
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
On Fri, Nov 11, 2011 at 11:36, Simon Ochsenreither
<simon.och...@googlemail.com> wrote:
--
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
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-----
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
Sorry for ignorance, where is it deprecated?
This is Scala 2.10. And in 2.11 it will be an Int 3 without warnings. Correct?