www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - reddit discussion on article by bearophile

reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
http://www.reddit.com/r/programming/comments/me6a5/some_examples_of_strong_static_typing_in_d/

Andrei
Nov 16 2011
next sibling parent reply Kagamin <spam here.lot> writes:
Andrei Alexandrescu Wrote:

 http://www.reddit.com/r/programming/comments/me6a5/some_examples_of_strong_static_typing_in_d/
 
 Andrei

Those are examples of shitty C coding... if I wanted something akin to what he wrote I'd do this #define ENTRY(name, val) { (1<<val), #name }, then do struct { unsigned long val; const char *name; } entries[] = { ENTRY(0, yellow) ENTRY(1, blue) ... }; Haha... He's unlucky if this compiles (and it can).
Nov 16 2011
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 11/16/11 10:34 AM, Kagamin wrote:
 Andrei Alexandrescu Wrote:

 http://www.reddit.com/r/programming/comments/me6a5/some_examples_of_strong_static_typing_in_d/

 Andrei

Those are examples of shitty C coding... if I wanted something akin to what he wrote I'd do this #define ENTRY(name, val) { (1<<val), #name }, then do struct { unsigned long val; const char *name; } entries[] = { ENTRY(0, yellow) ENTRY(1, blue) ... }; Haha... He's unlucky if this compiles (and it can).

Heh heh... took me a few seconds to figure out what you meant. Andrei
Nov 16 2011
parent reply Kagamin <spam here.lot> writes:
Andrei Alexandrescu Wrote:

 Heh heh... took me a few seconds to figure out what you meant.

It actually DOES compile because `yellow` and `blue` are in enum previously defined with their respective values 1<<0 and 1<<1, he tried to construct a value-to-string map.
Nov 16 2011
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 11/16/11 11:42 AM, Kagamin wrote:
 Andrei Alexandrescu Wrote:

 Heh heh... took me a few seconds to figure out what you meant.

It actually DOES compile because `yellow` and `blue` are in enum previously defined with their respective values 1<<0 and 1<<1, he tried to construct a value-to-string map.

This is actually a pretty awesome fail. Andrei
Nov 16 2011
parent reply bearophile <bearophileHUGS lycos.com> writes:
Andrei Alexandrescu:

 This is actually a pretty awesome fail.

Derived from those thoughts, an idea for Phobos, a bitFlags function: http://d.puremagic.com/issues/show_bug.cgi?id=6946 A desire: http://d.puremagic.com/issues/show_bug.cgi?id=6916 Example: import std.stdio; enum : int { A, B } void main() { writeln(B); // I'd like it to print "B" here. } I think this is related to: http://d.puremagic.com/issues/show_bug.cgi?id=5004 Bye, bearophile
Nov 17 2011
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 11/17/2011 09:33 AM, bearophile wrote:
 Andrei Alexandrescu:

 This is actually a pretty awesome fail.

Derived from those thoughts, an idea for Phobos, a bitFlags function: http://d.puremagic.com/issues/show_bug.cgi?id=6946 A desire: http://d.puremagic.com/issues/show_bug.cgi?id=6916 Example: import std.stdio; enum : int { A, B } void main() { writeln(B); // I'd like it to print "B" here. }

Hmm. How would that work? A and B don't have an own type. Your program is equivalent to import std.stdio; enum A = 0; enum B = 1; void main() { writeln(B); // prints 1 because B is replaced with 1. } You could use mixins to automatically generate alias E.A A; alias E.B B; // ... for a given enumerated type. import std.stdio, ...; enum E : int { A, B } mixin Import!E; void main() { writeln(B); // prints "B" }
Nov 17 2011
parent bearophile <bearophileHUGS lycos.com> writes:
Timon Gehr:

 Hmm. How would that work? A and B don't have an own type.
 Your program is equivalent to
 
 import std.stdio;
 enum A = 0;
 enum B = 1;
 void main() {
      writeln(B); // prints 1 because B is replaced with 1.
 }

Right, my mental model of nameless enums was very wrong. I was thinking of them more like symbols (all nameless enums aren't grouped under a common name).
 You could use mixins to automatically generate
 alias E.A A;
 alias E.B B;
 // ...
 
 for a given enumerated type.

Right, but I don't know if I will ever need this. Bye and sorry, bearophile
Nov 17 2011
prev sibling next sibling parent reply Somedude <lovelydear mailmetrash.com> writes:
Le 16/11/2011 17:57, Andrei Alexandrescu a écrit :
 http://www.reddit.com/r/programming/comments/me6a5/some_examples_of_strong_static_typing_in_d/
 
 
 Andrei

Someone brought up a problem I had thought about a few days ago. I quote (emphasis by me):
 However, I am not much of a fan of D. I prefer c++ more because it

not like this because you end up using libraries which use the GC* and I think that ruins a sort of elegance you get from select right kind of smart pointer for as specific usage. Given that D boasts that you can choose to *not* use the GC, shouldn't Phobos strive to resort to manual memory management ? I supposed this has already been discussed, but just in case I missed the discussion, could someone give a link to it ?
Nov 16 2011
parent Jesse Phillips <jessekphillips+d gmail.com> writes:
On Wed, 16 Nov 2011 22:59:35 +0100, Somedude wrote:

 Given that D boasts that you can choose to *not* use the GC, shouldn't
 Phobos strive to resort to manual memory management ?
 
 I supposed this has already been discussed, but just in case I missed
 the discussion, could someone give a link to it ?

I don't know any links off hand, but it has been concluded with a couple points. - When you have small limited memory, then custom containers and algorithms would be created for the task at hand. - When performance is needed, allocating upfront is what's needed. - The compiler, I believe confirmed by Walter, will have a switch which warns/errors when using GC expecting features. - Phobos can be extended to make manual management easier. So we just need some people to pick up the last two and create some pull requests.
Nov 17 2011
prev sibling parent reply Caligo <iteronvexor gmail.com> writes:
--bcaec53aeaaa47f75c04b1f9fbe4
Content-Type: text/plain; charset=ISO-8859-1

On Wed, Nov 16, 2011 at 10:57 AM, Andrei Alexandrescu <
SeeWebsiteForEmail erdani.org> wrote:

 http://www.reddit.com/r/**programming/comments/me6a5/**
 some_examples_of_strong_**static_typing_in_d/<http://www.reddit.com/r/programming/comments/me6a5/some_examples_of_strong_static_typing_in_d/>

 Andrei

Those nested for-loops are painful to look at. --bcaec53aeaaa47f75c04b1f9fbe4 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Wed, Nov 16, 2011 at 10:57 AM, Andrei= Alexandrescu <span dir=3D"ltr">&lt;<a href=3D"mailto:SeeWebsiteForEmail er= dani.org">SeeWebsiteForEmail erdani.org</a>&gt;</span> wrote:<br><blockquot= e class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc sol= id;padding-left:1ex;"> <a href=3D"http://www.reddit.com/r/programming/comments/me6a5/some_examples= _of_strong_static_typing_in_d/" target=3D"_blank">http://www.reddit.com/r/<= u></u>programming/comments/me6a5/<u></u>some_examples_of_strong_<u></u>stat= ic_typing_in_d/</a><span class=3D"HOEnZb"><font color=3D"#888888"><br> <br> Andrei<br> </font></span></blockquote></div><br><div>Those nested for-loops are painfu= l to look at.</div> --bcaec53aeaaa47f75c04b1f9fbe4--
Nov 17 2011
parent bearophile <bearophileHUGS lycos.com> writes:
Caligo:

 Those nested for-loops are painful to look at.

I think they aren't so bad in that code. Problems like that can be solved with hard-coded code or with generic and more flexible code. The code shown is more toward hard-coded. Feel free to write a different solution, with fewer nested loops. Bye, bearophile
Nov 17 2011