Allegro.cc - Online Community

Allegro.cc Forums » Off-Topic Ordeals » the D programming language

This thread is locked; no one can reply to it. rss feed Print
the D programming language
Matthew Leverton
Supreme Loser
January 1999
avatar

(Warning, this is somewhat of a blog post. You can safely skip down to the question at the end if it bores you.)

I've been playing around with the D language for the past couple of days, and I have to say I'm very impressed. My interest is twofold: I'm looking for a language to write my web framework in and I'm interested in using it with Allegro.

For web programming, I've come from a heavy background in scripting with classic ASP (ugh) and PHP. While I like PHP a lot, it (and haphazard scripting in general) breaks down for large scale, framework style of applications. That's not to say it's out of PHP's league, but one can only take so much of the $this->foo syntax before officially going insane. And I've never cared much for Java, so I haven't even given C# a chance.

Unlike Java, D compiles to native code and can (almost) directly interface with C libraries (hence, DAllegro). The first thing I did was create my own wrapper class around libmysqlclient to mimic the style of database access I've come to enjoy with PHP. A side by side comparison:

PHP                                   D
$db = new DB();                       DB db = new DB();
$db->execute("UPDATE foo");           db.execute("UPDATE foo");
$foo = $db->getOne("SELECT foo");     char[] foo = db.getOne("SELECT foo");

$row = $db->getRow("SELECT *");       DBRow row = db.getRow("SELECT *");
echo $row['foo'];                     writef("%s", row["foo"]);

foreach ($db->getAll($sql)) as $row)  foreach(row; db.getAll(sql))
  echo $row['foo']."\n";                writef("%s", row["foo"]);

Obviously C++ can do the same thing, but D is so concise. While I sometimes use C++ for the STL, it is so ugly...

The nice thing about writing in D, as opposed to PHP, is you can bind results directly to variables (without having to go through some invisible conversion):

DBStatement st = db.prepare("SELECT id,name FROM foo");
st.execute();

int id;
char[] name;

st.bindResult(1, id);
st.bindResult(2, name);

while(st.fetch())
{
  writefln("Person #%d's name is %s.", id, name);
}

D can pretty much do everything PHP can do right out of the box, which is impressive considering that it's a compiled language. (Obviously, I'm talking about core language features, not the plethora of PHP C Modules.)

// associative arrays
int[char[]] foo;
foo["pizza"] = 1;

// dynamic arrays
int[] foo;
foo ~= 1;  // foo[0] = 1

While D has no "string" object, it doesn't really need one because of how flexible the arrays are.

char[] foo;
foo = "Hello";
foo ~= ", World";

For a test, I translated the a.cc database indexer line by line from PHP to D. On my Linux VM, it ran almost twice as fast. On the live server, it actually runs the same speed, but it's possible that the database is the bottleneck. Of course, I'm also doing some PHP-like things, so that doesn't help D out.

All-in-all, it really seems nice. In fact, I'd say that D is what C++ should have been. Everything it does is so simple compared to C++...

The Question at the End: Does anyone here use D? Have any favorite features or gripes to share? Why, except for legacy and maturity purposes, would anyone use C++ over D? (I realize it doesn't have many tools, IDEs, etc... I'm asking about the language itself.)

Thomas Fjellstrom
Member #476
June 2000
avatar

From this and other reviews, I will be trying D out in the near future. And I can't wait till they manage to get some form of C++ support so I can use it with Qt. QtD would rock SO HARD its unimaginable.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Onewing
Member #6,152
August 2005
avatar

First off, and I know this is a bad argument, but why do they call it D? That's a horrible name. I'd much rather it be called B or something cute like BFlat or BMinus.

:-X

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Jonatan Hedborg
Member #4,886
July 2004
avatar

Haven't tried it, seems very nice. How does it handle function pointers? (i use PHP's "string with function name as function-pointer"-thingy in a CMS I'm working with/on)

EDIT: onewing; there is already a A and B. Hence C. And now D :)

nonnus29
Member #2,606
August 2002
avatar

I agree with Thomas, I haven't tried it out formally but from all reports D seems very sweet indeed. When I did install a long time ago all the little differences between it and Java gave me the willies.

Your examples are very 'svelte'. The langauge seems awesome. But (and I mean B.U.T.) the lack of library support (or the lack of extensive libraries) is a real hindrance in my opinion. I know you said not to mention this, but in Java there's a library for damn near everything, and often more than one. To me this is to important to ignore.

Edit: I don't mean to campaign for one language over another, I simply mean to say that library support is VERY important in this day and age.

From the regard of cool languages with great library support I currently favor Scala because it mixes SML style constructs and mixins (?) into a language that compiles to Java or .NET bytecode giving access to each platforms extensive libraries.

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

How does it handle function pointers?

Quite well, it has (anonymous) Delegates, Dynamic Closures, Function Literals and plain Function Pointers. The first few are much more usefull than pure function pointers imo.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Goalie Ca
Member #2,579
July 2002
avatar

I remember evaluating D a while back.. and all i can remember is the conclusion. D is better but i still think it took the wrong direction.

Right now python is my miracle language. The only thing i haven't checked out yet is boo or ironPython.net. Supposedly those are nice and fast.

C++ is my main workhorse language. It takes a really good coder to make c++ fun and easy to write. The biggest problem with c++ is the lack of true documentation... you know all those little things that programmers have in their head... such as why they chose to do it this way and what consequences this has. I've created a framework for my thesis where it pretty much boiled down to simple clean constructs but the important part is that it is fast and can run on clusters. Extensive use of stl algorithms but not containers. Ironic eh? but it came out really really clean and fast.

Haskell is becoming my "i have a really clever math idea.. lemme test it out" language. Haskell is beautiful to work with graphs or parsing. It is pretty simple and elegant but requires a real appreciation for the underlying math. It's one of those languages only a mathematician/computer scientist can love. I'm an engineer but i love it for its elegance. I just haven't found how to incorporate it into my daily workhorse language.

-------------
Bah weep granah weep nini bong!

Matthew Leverton
Supreme Loser
January 1999
avatar

I dislike the name because its hard to Google for D specific articles. Other than that, I think it's fine and sensible.

Quote:

Haven't tried it, seems very nice. How does it handle function pointers? (i use PHP's "string with function name as function-pointer"-thingy in a CMS I'm working with/on)

Function pointers are called delegates. It also has references and lazy parameters (not evaluated until used). So you almost get the Javascript style of dynamic closures, although it's not quite as powerful. (The scope ultimately gets lost, although this is something that may change in later versions.) However, even as it is now, it has better support than PHP.

D also has real pointers, but you rarely need to use them. For instance:

void foo(inout int a)
{
  a = 1;
}

int x = 0;
foo(x);
// x = 1

Quote:

But (and I mean B.U.T.) the lack of library support (or the lack of extensive libraries) is a real hindrance in my opinion.

But it can interface with C libraries very easily:

extern (C) {
  void library_func(int a); 
}

int main()
{
  int a;
  library_func(a);
}

You can link directly with the .lib file made in C. (If under Windows, the library must be dynamic or built with the Digital Mars C compiler.)

Onewing
Member #6,152
August 2005
avatar

Quote:

Other than that, I think it's fine and sensible.

In the scheme of things following the A, B, C, etc. method, D has negative connotation for me. I mean, what's next, F? A language that auto-fails? Now, if they put a colon at the beginning of D, it might be a little better (: + D = :D). There's no negativity in that.

Anywho, I've seen a thread or two around here on D, one about porting allegro to D. It seems to be getting a lot of compliments and I suppose I should check it out before I end up behind.

How OOP can you go with D?

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

How OOP can you go with D?

Basically, D is C++++. Has better support for everything C++ can do, and more (3rd party library support nonwithstanding).

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Matthew Leverton
Supreme Loser
January 1999
avatar

D uses single inheritance with multiple interfaces, mixins, templates, and operator overloading.

I never used templates much in C++ because they are so ridiculously ugly... Does C++ have an equivalent for this?

void templateFunc(T) (T value)
{
  static if (value.typeof.stringof == "int") // sorry if there's a better way to express this
  {
    writefln("Hello!");
  }
}

templateFunc(1000); // prints Hello

templateFunc("Hi"); // nothing

Basically, the static if happens at compile time and the check goes away. (Obviously, you could overload the function in C++, but my example is trivial on purpose.)

Can you do that with the preprocessor and templates in C++? (That's another thing. D has no preprocessor, because it provides elegant solutions for when you normally would have used one.)

Oh, and in addition to all this high level goodness, you can drop into inline assembler too. ;D

bamccaig
Member #7,536
July 2006
avatar

Matthew Leverton said:

And I've never cared much for Java, so I haven't even given C# a chance.

I haven't had the chance to learn D, but I have done about 10 C# tutorials and I must say I'm very pleased with it. Looking at your D examples I've gotta say I prefer C# syntax (though the differences in your simple examples look minimal).

The only catch about C# is that it requires a .NET CLR to run, but Mono provides that for *nix environments so it's not a big deal.

I suggest don't avoid C# because of Java; you'll be missing out. I hate Java, but C# is a beautiful language.

Onewing said:

In the scheme of things following the A, B, C, etc. method, D has negative connotation for me.

Seconded... :-/

Matthew Leverton
Supreme Loser
January 1999
avatar

Regarding C#, I almost commented that I also never tried it because it's essentially controlled by Microsoft and I don't want to be locked in to Windows or have to use a sub-standard open-source implementation. But, I didn't want the thread to turn into an argument over whether or not Microsoft controls C# and if C# on other platforms is sub-standard.

The point is to discuss the good and bad of D. And other than the stuff that would hinder any new language (no IDE, no libraries, etc), I've not come across anything that I've not liked. And personally, I enjoy writing wrapper classes over top existing C libraries, so the lack of official libraries for D doesn't really bother me.

bamccaig
Member #7,536
July 2006
avatar

Matthew Leverton said:

void foo(inout a)

What's with inout? :-/ Is that a datatype or a keyword...? :-/ I don't like it, whatever it is...

Matthew Leverton
Supreme Loser
January 1999
avatar

I should have had a type. (example fixed) The new name is actually "ref" (for reference) but the dmd compiler doesn't support it yet.

bamccaig
Member #7,536
July 2006
avatar

Matthew Leverton said:

I should have had a type. (example fixed) The new name is actually "ref" (for reference) but the dmd compiler doesn't support it yet.

ref is a lot better than inout. ;D Sounds like D is going to be an awesome language, but I think I'll wait a while to try it out. :-/ I'll be busy with C# and networking/multi-threading/game programming in C++ for a while... That doesn't include 71/2 hours of Web technologies every weekday... :-/

nonnus29
Member #2,606
August 2002
avatar

Quote:

What's with inout? :-/ Is that a datatype or a keyword...? :-/ I don't like it, whatever it is...

Inout is a valid parameter passing style; the value is copied to the function when the function is called and copied back when the function returns. It should be exactly like passing by reference but in some cases it can yield different results. I think it came from Fortran originally, or maybe Algol?

All I know is, when I finish this compiler I'm writing in C I'll never write another line of C as long as I live.... :'( Is there a lex/flex implementation for D?

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

All I know is, when I finish this compiler I'm writing in C

I forget (if I ever knew), what language is the compiler for?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

BAF
Member #2,981
December 2002
avatar

Are you porting your web framework to D and doing all your web stuff in D?

Jonny Cook
Member #4,055
November 2003

A while ago I looked at D, but I wasn't feeling very adventurous so I didn't get past the FAQ/Documentation/D-C++ comparison. But it seemed like a very good language to me. If an Allegro port is ever made for it I'll definitely try it out, but until then I don't find myself having any use for it (at class we use Java exclusively, and other than that I just do game programming).

The main reason I'm reluctant to use D (if this could be called a reason) is that I'm scared of becoming, well... lazy. It might just be because I started with C/C++ 4 years ago and have pretty much used it exclusively since then, but I feel like I'm somehow cheating by using a language which makes it easier to program than it is in C++. I really like how C++ forces you to have good programming practices, because if you don't you will suffer the consequences, and programming with good practices is very important to me. And of course this is just my opinion as a hobbyist programmer who never has to deal with a deadline (or even finish anything I start for that matter).

Or it could be that I'm too lazy to choose to have good programming practices, and thus place it in the hands of my programming language to force me.

Regarding C# (just a quick comment), I had to learn it recently for a project I'm helping out with and so far I'm very impressed. It's just an all around better language than Java. While Java is a nice language in some aspects (for one thing it's easy to use), it just feels too cumbersome and restrictive to program with comfortably. C# on the other hand feels a lot more like C++ to me, but with ease of use of Java and all of the flexibility of both combined. It also has really cool reflection support.

The face of a child can say it all, especially the mouth part of the face.

nonnus29
Member #2,606
August 2002
avatar

Quote:

I forget (if I ever knew), what language is the compiler for?

NASIC == Not bASIC

I write about it in my blog. I've got the parser pretty much finished, it builds a syntax tree from a program and I can pretty print a program back out. I'm working on improving error reporting and inserting type coercions. It'll run thru an interpreter. That's the next part I have to implement.

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

If an Allegro port is ever made for it I'll definitely try it out

You wouldn't happen to be talking about this would you?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Matthew Leverton
Supreme Loser
January 1999
avatar

Quote:

The main reason I'm reluctant to use D (if this could be called a reason) is that I'm scared of becoming, well... lazy.

To me, C++ has always just felt like it was tacked on to C with barely any consideration. I think you'd find that D is like an organized, concise version of C++ with memory management. The very basics of the STL (like list, vector, map) are rolled into native types. To me, any language that can be compiled to machine code is far from "cheating."

Quote:

If an Allegro port is ever made for it I'll definitely try it out

As Thomas has linked, it already exists. Again, D can use any C library with only a little bit of work. (I don't mean to trivialize the work done on DAllegro, because writing import files for all the Allegro functions would be tedious.)

Quote:

Is there a lex/flex implementation for D?

Not that I know of. What are you using now?

Quote:

Are you porting your web framework to D and doing all your web stuff in D?

I'm just in the experimental stages right now. I do some pretty funky stuff in my PHP template system, but I think I can imitate it in D. I've created the DB wrapper for MySQL, which was easy enough. Redoing the template parser and "compiler" would take the most work. Of course, then there's the whole web interface. dsource has a FastCGI project that I'd probably start with. Also, DMDScript (a D implementation of the ECMA 262 scripting language) is intriguing.

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

Redoing the template parser and "compiler" would take the most work.

How would you actually handle this bit? call the compiler dynamically? or some wacky vm setup?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Matthew Leverton
Supreme Loser
January 1999
avatar

Preferably, I'd convert the template into a Javascript file (on first run after modification) and execute it through DMDScript. Or I could create my own mini byte-code. There's really not much going on in a template file other than: print, loop, if, include, and get var.

I really don't think I would want to compile the templates to machine code. That's a lot of messing around for only a little gain.



Go to: