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

Quote:

But seriously, is there anything in D that you cannot do in C++?

It depends on your perspective.

Some people say you can do anything in Assembler that you can do in C. Others say you can do anything in C that you can do in C++. And long those lines, you could say that C++ can do anything that you can do in D.

So I think the question should really be, "what things are D better at?" And so far, my answer would be "everything." I'm not an expert C++ user, so I'm not extremely qualified to answer, but here are a few things that D makes easy. (Sorry for the silly examples.)

Dynamic arrays:

int[] array;
array ~= 42; // array[0] = 42

Associative arrays:

int[char[]] map;
map["foo"] = 1;

Foreach:

foreach(val; array)
{
  writefln("%s", val);
}

Delegates:

int foo(bool delegate(int) func)
{
  if(func(42))
  {
    writefln("Hello, World!");
  }
}

foo((i) { return i < 100 ? true : false;  });

Templates:

void my_template(U) (U bar)
{
 static if (U.typeof == int) { /* or something like that */
    // code specific to an integer type
 } 

 // generic code
}

my_template("Hello"); // works with strings
my_template(42);

And it has memory management, no need for pointers (unless you are using a C library), no need (or support) for #include files or the preprocessor, etc.

I think it's safe to say that most of the stuff can be implemented in C++ with more verbose syntax or the use of ugly macros. It's not meant to be radically different. To me, the biggest advantage over C++ is that everything feels so much more clean and natural.

It's well documented. Read up on it yourself.

Simon Parzer
Member #3,330
March 2003
avatar

Quote:

If you were to write the framework in .NET 1.1, you could actually (easily) compile the code to .NET 1.1 executables. Since you used 1.1 and not 2.0, Mono could then actually compile them to native images.

Isn't .NET a framework of it's own? Does it make sense to use a framework to write a framework?

Goalie Ca
Member #2,579
July 2002
avatar

The main point for me to use a c++ like-language is because

1) C++ is transparent. Users must know every implementation detail. There's never any doubt how a list is implemented or passed into a function. This sounds like a bad thing, but its really a good thing. C++ is a systems engineering language.
2) Performance! Even then there are cases where i still fall back to c.
3) Low-level control. C++ is used for engineering. I somehow doubt that "next greatest language" D would make a nice systems engineering language.

There are so many languages out there. In the big scheme of things if one were to give up c++ and more do D, what is the real gain? The languages aren't all that different from each other. While i'm assuming D has some advantages with syntactic sugar, it probably also has some disadvantages in other areas. I don't see D stomping on c++'s niche any time soon.

Python is a language that is sufficiently different but interfaces with C++ extremely well. In python you care about writting code to do a task and you are not too worried about details. Because of that, the syntax is much cleaner, the typing system works differently, it is interpreted, etc. It covers a completely different area. If borrows some ideas from functional programming and a lot of ideas from all kinds of other languages. I consider python a glue language because it provides a brilliantly simple way to make a program or script or whatever out of libraries and functions. I have even started using it is a matlab replacement to some extent.

Haskell is a language that is on a completely different plane. It has this amazing static type checking and code verification tools. If it compiles it probably works. Implementation details are not reflected in the language at all. It is entirely up to the compiler to translate your statements into working instructions. You write pure mathematical stateless functions and they always return the same result. This is an excellent language for thinking about math and computer science problems. It can optimize very easily and parallelizes for free (no locking code since no variables).

In summary, C++ is very strongly imperative. D also seems very much that way. Of course there are differences but how much can really be gained by switching to D.
the big question everyone is asking lately is how can you deal with concurrency?
Well google wrote a famous paper on map-reduce algorithms for C++ borrowed from functional programming. And if i remember tim sweeney from epic (unreal 3!) proposes to use functional programming techniques. C++ can work with all of these big paradigms with the right library. It is not pure by any means but it can be pure enough for the problems faced in many cases.

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

Matthew Leverton
Supreme Loser
January 1999
avatar

Quote:

1) C++ is transparent. Users must know every implementation detail. There's never any doubt how a list is implemented or passed into a function. This sounds like a bad
thing, but its really a good thing. C++ is a systems engineering language.
2) Performance! Even then there are cases where i still fall back to c.
3) Low-level control. C++ is used for engineering. I somehow doubt that "next greatest language" D would make a nice systems engineering language.

I don't see how D fails any of those points.

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

Isn't .NET a framework of it's own? Does it make sense to use a framework to write a framework?

PHP is a framework, too. So is C++. It makes sense to use the C++ standard library when building another framework.

And speaking of that, for me language selection is all about the standard library. (Well, for compiled languages. I hate interpreted languages.) Really, between C++, C#, and what I've seen of D, I prefer C#, because it has a huge standard library. If D had a comparative standard library, I might consider developing in it.

What it boils down to is that syntax is syntax. No amount of syntax is going to serious slow down development. Hunting down rarely-used and unsupported libraries on the net and wading through Makefiles/project files/whatever to get them installed for every development machine is where my time gets wasted.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Goalie Ca
Member #2,579
July 2002
avatar

Quote:

I don't see how D fails any of those points.

But is it any better? Is there a compelling reason to switch to D or is it just "a little bit cleaner".

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

Matthew Leverton
Supreme Loser
January 1999
avatar

I don't think anyone would argue that a person should switch from C++ to D in the sense of rewriting all of his projects.

But if you're starting something from scratch and are the type of person (like me) who's always been frustrated with how ridiculous C++ is, then I think D is worth using.

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

But is it any better? Is there a compelling reason to switch to D or is it just "a little bit cleaner".

It's a LOT cleaner. IMO thats more than enough reason ;)

--
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

Michael Faerber
Member #4,800
July 2004
avatar

Anybody else here waiting for C++0x?

--
"The basic of informatics is Microsoft Office." - An informatics teacher in our school
"Do you know Linux?" "Linux? Isn't that something for visually impaired people?"

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

Anybody else here waiting for C++0x?

Latest GCC is supposed to already support it. :D

--
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

Jonatan Hedborg
Member #4,886
July 2004
avatar

How does it compare to Java?

(let's not make this a Java vs. X thread btw, I'm just asking)

Sevalecan
Member #4,686
June 2004
avatar

Quote:

Anybody else here waiting for C++0x?

Yes.

Quote:

Latest GCC is supposed to already support it.

Yay.

Now I need to go and find out what's new.

[edit]

Isn't it supposed to be done around 2009? If they haven't decided on everything yet...

Edit2:

Yeah, I guess there is experimental support.

TeamTerradactyl: SevalecanDragon: I should shoot you for even CONSIDERING coding like that, but I was ROFLing too hard to stand up. I love it!
My blog about computer nonsense, etc.

nonnus29
Member #2,606
August 2002
avatar

Quote:

Matthew said:
But there's a lot of higher level things I'd like to add that are just impractical to do. (I'd like to add things to the core language.) So what I've considered is writing my own custom language that just gets translated to PHP, similarly to what I do with the templates.

Then Domain Specific Languages are for you! This is EXACTLY why I'm interested in writing compilers and languages. Take for example this problem: suppose you're confronted with rewriting a legacy appication consisting of about 250 screens/pages. Some are simple CRUD but many have complex business logic behind them. The only way I can think of for it to even be possible is to create a DSL specific for the problem.

Quote:

CGames said:
If you were to write the framework in .NET 1.1, you could actually (easily) compile the code to .NET 1.1 executables. Since you used 1.1 and not 2.0, Mono could then actually compile them to native images.

This is entirely feasible and even desirable. And for a Java example you can use frameworks like Terracotta to get automatic parallelization across many servers. Not sure if .NET has this capability. "Simply" compile your langauge to C# or Java and take advantage of 1) the much better opimizing compilers and 2) the frameworks that exist for those platforms.

Quote:

Matthew said:
But if you're starting something from scratch and are the type of person (like me) who's always been frustrated with how ridiculous C++ is, then I think D is worth using.

Amen, But to use D for your template base langauge, wouldn't you need a mod_D apache module or something?

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

wouldn't you need a mod_D apache module or something?

Nope. any executable can be called from the server as a CGI app. Tis what cgi is for. and mod_php doesn't actually have any real advantages over cgi php since it doesn't seem to do any fancy caching. unlike mod_perl.

--
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:

Then Domain Specific Languages [en.wikipedia.org] are for you! This is EXACTLY why I'm interested in writing compilers and languages.

Yes, I have a strong interest in that. Even if it is just as a hobby.

I'm actually in the process of writing a PHP VM in D. I'm writing a lexer that turns it into tokens. Then I'm taking the tokens and turning them into byte code. From there, it will run in my VM. It (the PHP aspect) is just for educational purposes, unless it ends up having decent performance. It would be cool if my framework had the ability to run PHP natively, but that's not really what I'm trying to do. (I'd have to "port" the popular PHP extensions into D libs as well.)

Quote:

Amen, But to use D for your template base langauge, wouldn't you need a mod_D apache module or something?

Writing a proper Apache module would be beneficial, but using FastCGI is probably sufficient. In fact, lighttpd's FastCGI is supposedly faster than Apache's mod_php. (If you trust biased benchmarks.)

FastCGI for D already exists, and that's what I would begin with. As you are implying, there is more ground work that would need to be done to use D for this, but as someone who is very particular about code, that doesn't really bother me at all.

ThePredator
Member #8,646
May 2007

I tried it but Ada still kicks it's ass up and down (so does Eiffel).

But I will seriously cry if D gets more library support (Ada keeps getting passed by as far as libraries go).

Kitty Cat
Member #2,815
October 2002
avatar

Quote:

Dynamic arrays:

int[] array;
array ~= 42; // array[0] = 42

I'm not even sure I see how that works. ~ is the invert operator (in C(++)), and placed like that looks like 'array sorta-kinda-equals 42' which makes no sense with what you say it's doing, so it's quite weird for someone coming from C(++).

Quote:

Associative arrays:

int[char[]] map;
map["foo"] = 1;

map<string, int> blah;
blah["foo"] = 1;

C++ also allows it to use user-specified comparitors by adding an extra value to the map template args.

Quote:

Foreach:

foreach(val; array)
{
  writefln("%s", val);
}

Finally, a useful feature, even if it's only a syntax cleanup..

Quote:

Delegates:

int foo(bool delegate(int) func)
{
  if(func(42))
  {
    writefln("Hello, World!");
  }
}

foo((i) { return i < 100 ? true : false;  });

Unnamed functions? Interesting. Can't see much use though, especially given how ugly that looks (could just break it out into a real function and use a function pointer to the same effect..).

Quote:

Templates:

void my_template(U) (U bar)
{
 static if (U.typeof == int) { /* or something like that */
    // code specific to an integer type
 } 

 // generic code
}

my_template("Hello"); // works with strings
my_template(42);

Compile-time template type checks are something I've wanted. Isn't C++0x supposed to have that?

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

bamccaig
Member #7,536
July 2006
avatar

Kitty Cat said:

I'm not even sure I see how that works. ~ is the invert operator (in C(++)), and placed like that looks like 'array sorta-kinda-equals 42' which makes no sense with what you say it's doing, so it's quite weird for someone coming from C(++).

IIRC, somebody said that in D the tilde (~) is a concatenation operator which suggests that it adds another element to the array with the value 42.

    int[] array;
    array ~= 42; // array[pre_length] = 42;

Matthew Leverton
Supreme Loser
January 1999
avatar

Quote:

I'm not even sure I see how that works. ~ is the invert operator (in C(++)),

~= is the D concat operator for arrays. So:

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

writefln("%s", foo); // outputs "Hello, World";

writefln("%s", foo[7 .. length]); // outputs "World" via array slicing

Quote:

C++ also allows it to use user-specified comparitors by adding an extra value to the map template args.

You can also do this:

class Foo
{
}

int[Foo] map;

Foo key = new Foo();
map[key] = 1;

You do have to define a couple of methods in Foo so that it knows how to create hashes, etc.

Quote:

Unnamed functions? Interesting. Can't see much use though, especially given how ugly that looks

Lambda expressions are huge in languages like Ruby and JavaScript. You can do a lot with them, but it takes a different style of programming to maximize their benefits.

bamccaig
Member #7,536
July 2006
avatar

Matthew Leverton
Supreme Loser
January 1999
avatar

Another nice little thing:

  RegExp reg = new RegExp(r"([a-z0-9]+)(@(\d+))?", "i");

If you prefix a string with r, you get exactly what you see.

Billybob
Member #3,136
January 2003

Curious question: Does D have any built-in support for multi-threading?

Joel Pettersson
Member #4,187
January 2004

Though I've only looked thus far, D certainly seems a better higher-level language. A "better C++", so to say. (and one which doesn't go as rigidly in the "wrong direction" as Java, tying my hands behind my back and causing endless frustration) On the other hand, it's a much "worse C".

Should it be widely adopted at the cost of C++, maybe pure C - remaining interfaceable with D - will see a re-increase in popularity for lower-level coding, along with a separation of language-use depending on what is to be done rather than using the half-arsed compromise of C++. Wouldn't be bad at all.

relpatseht
Member #5,034
September 2004
avatar

Billybob said:

Curious question: Does D have any built-in support for multi-threading?

From what I have seen, the extent of its multithreading capabilities are the synchronized parameter. To quote the D overview:

Multithreaded programming is becoming more and more mainstream, and D provides primitives to build multithreaded programs with. Synchronization can be done at either the method or the object level.
synchronized int func() { ... }
Synchronized functions allow only one thread at a time to be executing that function.

The synchronize statement puts a mutex around a block of statements, controlling access either by object or globally.

Matthew Leverton
Supreme Loser
January 1999
avatar

There is the std.thread module, but I've not used it yet.



Go to: