www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Octal Literals

reply "Dave X." <dxuhuang gmail.com> writes:
I'm a fresh college graduate who just got a job as a software 
developer, and I have been enthusiastically watching D for a 
while now (I program primarily in Java and C). I have some 
functional programming experience in Haskell and Scala as well.

I like using octal numbers, and I've always been interested in 
D's octal literals. I'm glad to see that the traditional syntax 
of C's octal literals is being replaced by a more readable one. 
However, I can't help but think that the template solution 
("octal!nnn") is a little too roundabout; is there a reason that 
that the "0o" prefix, which is already well established in 
languages like Haskell, OCaml, and Python, is not used?
Jul 17 2012
next sibling parent reply Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
On Tue, 17 Jul 2012 23:53:47 +0200
"Dave X." <dxuhuang gmail.com> wrote:

 I'm a fresh college graduate who just got a job as a software 
 developer, and I have been enthusiastically watching D for a 
 while now (I program primarily in Java and C). I have some 
 functional programming experience in Haskell and Scala as well.
 
I wish D had been as far along as it is now back when I was in college!
 I like using octal numbers, and I've always been interested in 
 D's octal literals. I'm glad to see that the traditional syntax 
 of C's octal literals is being replaced by a more readable one. 
 However, I can't help but think that the template solution 
 ("octal!nnn") is a little too roundabout; is there a reason that 
 that the "0o" prefix, which is already well established in 
 languages like Haskell, OCaml, and Python, is not used?
It was suggested a few times, but there was a lot of bikeshedding over it. Some people liked it, some hated it. One of the bigger objections was that octal literals were too rarely-needed to justify adding a new syntax into the language (this was at a time when D was far enough along that we were trying to start stabalizing the langauge rather than tossing in more stuff). The bikeshedding went around and around like that for awhile, during which time the awful old 0123 octal syntax remained. So when it was discovered that D's templates made it possible to implement octal literals in the library (octal!123), instead of in the language itself (0o123), that solved the deadlock and we went with it.
Jul 17 2012
parent reply "Dave X." <dxuhuang gmail.com> writes:
On Tuesday, 17 July 2012 at 22:35:48 UTC, Nick Sabalausky wrote:
 On Tue, 17 Jul 2012 23:53:47 +0200
 "Dave X." <dxuhuang gmail.com> wrote:

 I'm a fresh college graduate who just got a job as a software 
 developer, and I have been enthusiastically watching D for a 
 while now (I program primarily in Java and C). I have some 
 functional programming experience in Haskell and Scala as well.
 
I wish D had been as far along as it is now back when I was in college!
 I like using octal numbers, and I've always been interested in 
 D's octal literals. I'm glad to see that the traditional 
 syntax of C's octal literals is being replaced by a more 
 readable one. However, I can't help but think that the 
 template solution ("octal!nnn") is a little too roundabout; is 
 there a reason that that the "0o" prefix, which is already 
 well established in languages like Haskell, OCaml, and Python, 
 is not used?
It was suggested a few times, but there was a lot of bikeshedding over it. Some people liked it, some hated it. One of the bigger objections was that octal literals were too rarely-needed to justify adding a new syntax into the language (this was at a time when D was far enough along that we were trying to start stabalizing the langauge rather than tossing in more stuff). The bikeshedding went around and around like that for awhile, during which time the awful old 0123 octal syntax remained. So when it was discovered that D's templates made it possible to implement octal literals in the library (octal!123), instead of in the language itself (0o123), that solved the deadlock and we went with it.
Thanks! I guess I'll get used to it. Not that this really matters, but out of curiosity, how does this template work?
Jul 18 2012
next sibling parent travert phare.normalesup.org (Christophe Travert) writes:
"Dave X." , dans le message (digitalmars.D:172680), a écrit :
 Not that this really matters, but out of curiosity, how does this 
 template work?
By looking at the sources, if the template argument is a string, the program just compute the octal value as a human would do, that is it makes the sum of the digits multiplied by their respective power of 8. If the template argument is not a string, it is transformed into a string and the previous algorithm is used. -- Christophe
Jul 18 2012
prev sibling next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Wed, 18 Jul 2012 16:45:58 +0200, Dave X. <dxuhuang gmail.com> wrote:

 Not that this really matters, but out of curiosity, how does this  
 template work?
It converts the passed number to a string, then works on a digit at a time. Basically: foreach ( digit; number ) { if ( digit >= '0' && digit <= '9' ) result = result * 8 + digit; } -- Simen
Jul 18 2012
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Jul 18, 2012 at 05:06:21PM +0200, Simen Kjaeraas wrote:
 On Wed, 18 Jul 2012 16:45:58 +0200, Dave X. <dxuhuang gmail.com> wrote:
 
Not that this really matters, but out of curiosity, how does this
template work?
It converts the passed number to a string, then works on a digit at a time. Basically: foreach ( digit; number ) { if ( digit >= '0' && digit <= '9' ) result = result * 8 + digit; }
[...] And this is done at compile-time, so there is no runtime overhead (it just becomes a constant like an actual literal). T -- "640K ought to be enough" -- Bill G., 1984. "The Internet is not a primary goal for PC usage" -- Bill G., 1995. "Linux has no impact on Microsoft's strategy" -- Bill G., 1999.
Jul 18 2012
prev sibling next sibling parent Caligo <iteronvexor gmail.com> writes:
This article by Walter might be of an interest to you:

http://www.drdobbs.com/tools/user-defined-literals-in-the-d-programmi/229401068

On Tue, Jul 17, 2012 at 4:53 PM, Dave X. <dxuhuang gmail.com> wrote:
 I'm a fresh college graduate who just got a job as a software developer, and
 I have been enthusiastically watching D for a while now (I program primarily
 in Java and C). I have some functional programming experience in Haskell and
 Scala as well.

 I like using octal numbers, and I've always been interested in D's octal
 literals. I'm glad to see that the traditional syntax of C's octal literals
 is being replaced by a more readable one. However, I can't help but think
 that the template solution ("octal!nnn") is a little too roundabout; is
 there a reason that that the "0o" prefix, which is already well established
 in languages like Haskell, OCaml, and Python, is not used?
Jul 18 2012
prev sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Tuesday, 17 July 2012 at 21:53:50 UTC, Dave X. wrote:
 I'm a fresh college graduate who just got a job as a software 
 developer, and I have been enthusiastically watching D for a 
 while now (I program primarily in Java and C). I have some 
 functional programming experience in Haskell and Scala as well.

 I like using octal numbers, and I've always been interested in 
 D's octal literals. I'm glad to see that the traditional syntax 
 of C's octal literals is being replaced by a more readable one. 
 However, I can't help but think that the template solution 
 ("octal!nnn") is a little too roundabout; is there a reason 
 that that the "0o" prefix, which is already well established in 
 languages like Haskell, OCaml, and Python, is not used?
Coming from C++, the only time I've EVER seen octals used is "by accident" when developers try to 0-align his variables. Good riddance to the "0" prefix. Seriously >:( That said, I did not know of this "0o" prefix. It sounds like a good idea, and I see no reason not to add it, other than it is hard work for the compiler devs of course, who already have a lot of work. :)
Jul 18 2012
next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 7/18/12 10:29 AM, monarch_dodra wrote:
 I see no reason not to add it
This is never a good argument for adding things to a language. Andrei
Jul 18 2012
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Wednesday, 18 July 2012 at 17:37:02 UTC, Andrei Alexandrescu 
wrote:
 On 7/18/12 10:29 AM, monarch_dodra wrote:
 I see no reason not to add it
This is never a good argument for adding things to a language. Andrei
Taken out of context, no it isn't. "It sounds like a good idea", is a (relatively) good argument though.
Jul 18 2012
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 7/18/12 1:39 PM, monarch_dodra wrote:
 On Wednesday, 18 July 2012 at 17:37:02 UTC, Andrei Alexandrescu wrote:
 On 7/18/12 10:29 AM, monarch_dodra wrote:
 I see no reason not to add it
This is never a good argument for adding things to a language. Andrei
Taken out of context, no it isn't. "It sounds like a good idea", is a (relatively) good argument though.
Yah, thought the same after posting, sorry. Nevertheless, 0o or whatnot does not deserve, I opine, any attention. Andrei
Jul 18 2012
prev sibling parent reply "Daniel Murphy" <yebblies nospamgmail.com> writes:
"monarch_dodra" <monarchdodra gmail.com> wrote in message 
news:srtxvzubdafcjzwwnkmz forum.dlang.org...
 That said, I did not know of this "0o" prefix. It sounds like a good idea, 
 and I see no reason not to add it, other than it is hard work for the 
 compiler devs of course, who already have a lot of work. :)
It's a trivial amount of work to add '0o' octal literals to the language, but it was decided that octals are not used often enough to justify a language feature.
Jul 19 2012
parent reply "Dave X." <dxuhuang gmail.com> writes:
How was the reception of the idea of binary literals as opposed 
to octal (I think it's an awesome feature, I think D, OCaml and 
Java 7 are the only ones that have it)? How long did it take to 
decide to implement it?
Jul 19 2012
next sibling parent "Daniel Murphy" <yebblies nospamgmail.com> writes:
"Dave X." <dxuhuang gmail.com> wrote in message 
news:cokspgduvpyzcbioawud forum.dlang.org...
 How was the reception of the idea of binary literals as opposed to octal 
 (I think it's an awesome feature, I think D, OCaml and Java 7 are the only 
 ones that have it)? How long did it take to decide to implement it?
Binary literals were before my time, but AVR assembler also has them.
Jul 19 2012
prev sibling next sibling parent "David Nadlinger" <see klickverbot.at> writes:
On Thursday, 19 July 2012 at 14:13:06 UTC, Dave X. wrote:
 How was the reception of the idea of binary literals as opposed 
 to octal (I think it's an awesome feature, I think D, OCaml and 
 Java 7 are the only ones that have it)? How long did it take to 
 decide to implement it?
If they were introduced today, I guess they might well be implemented as a template too, similar to octal. David
Jul 19 2012
prev sibling parent Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
On Thu, 19 Jul 2012 16:13:05 +0200
"Dave X." <dxuhuang gmail.com> wrote:

 How was the reception of the idea of binary literals as opposed 
 to octal (I think it's an awesome feature, I think D, OCaml and 
 Java 7 are the only ones that have it)? How long did it take to 
 decide to implement it?
Binary literals have been there since way back around the beginning of D, just like hex literals, decimal literals and the now-dead old-style octal literals. Back when *everything* in D was a new feature. If octal literals had been like 0o123 or 0c123 instead of 0123 from the beginning, then I doubt we would have ever replaced them with octal!123 (since 0o123 or 0c123 aren't absolutely horrible like 0123 is).
Jul 19 2012