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
ImLeftFooted
Member #3,935
October 2003
avatar

I'm a bit late (so this might have been answered) but I'll push my luck anyway.

Quote:

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

Yes but its quite ugly and only works for classes. Theoretically you really don't need it for functions because of function overloading.

Quote:

(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++?

I'll make some example code (not tested and probably doesn't compile). Obviously just doing overloaded functions is preferred here, but we're trying to show what templates can do.

1template<typename T>
2class TemplateClass
3{
4 inline void templateFunc(T value)
5 {
6 // Do nothing
7 }
8}
9template< >
10class TemplateClass <int>
11{
12 inline void templateFunc(int value)
13 {
14 printf("Hello!\n");
15 }
16}
17 
18TemplateClass::templateFunc(1000); // prints Hello
19 
20TemplateClass::templateFunc("Hi"); // nothing

Billybob
Member #3,136
January 2003

ML and relpatseht, thanks for looking that up. Nice to know it has multi-threaded capability as well. Maybe I'll try to build a game with it some time in the future.

Thomas Fjellstrom
Member #476
June 2000
avatar

If it wasn't for wanting to use Qt in a few apps, I would so jump to D asap. :(

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

MiquelFire
Member #3,110
January 2003
avatar

I'm with Thomas. Matter of fact, I just made a first release of a QT app I was working on. Has some features to go before a 1.0 release however.

---
Febreze (and other air fresheners actually) is just below perfumes/colognes, and that's just below dead skunks in terms of smells that offend my nose.
MiquelFire.red
If anyone is of the opinion that there is no systemic racism in America, they're either blind, stupid, or racist too. ~Edgar Reynaldo

nonnus29
Member #2,606
August 2002
avatar

Yeah, I'm thinking of switching my compiler to D now... Thanks alot ML >:(

Walter Bright presentation:

http://video.google.com/videoplay?docid=-7073020265668105471

Myrdos
Member #1,772
December 2001

The two show stoppers for me are speed and library support. My understanding is that C++ tries to go as high level as possible without sacrificing speed. That is to say, a well-written C++ program should be 99% as fast as a C program.

Can D get the speed without the ugly syntax? It takes quite a bit of skill to use C++ well, and a mediocre programmer is going to pooch it. The hideous things my eyes have seen, modifying other people's programs. To me, a simpler language equals more robust code, because it takes less thinking to make good design decisions.

In fact, Bjarne Stroustrup seems to agree with ML.

Quote:

"Within C++, there is a much smaller and cleaner language struggling to get out". Yes, that quote can be found on page 207 of The Design and Evolution of C++. And no, that smaller and cleaner language is not Java or C#. The quote occurs in a section entitled "Beyond Files and Syntax". I was pointing out that the C++ semantics is much cleaner than its syntax.

If D can give me the semantics and speed of C++ with cleaner style, I'd consider switching.

[EDIT] Erm, the Digital Mars compiler is proprietary, that will slow adoption in the Linux realm. Plus, it only supports 32-bit systems. The GDC compiler is version 0.23 alpha, and only has one developer. I guess I'll give D a few years to mature.

__________________________________________________

HoHo
Member #4,534
April 2004
avatar

Quote:

If it wasn't for wanting to use Qt in a few apps, I would so jump to D asap. :(

Exactly the same here. I've been playing with QT for a few months now and I must say there is no other GUI toolkit as good that I've seen so far. I've also been reading a bit about D and it looks rather nice. Too bad those two things can't work together. Of cource the compiler being a bit too new doesn't help too much either.

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Matthew Leverton
Supreme Loser
January 1999
avatar

Walter Bright's official D compiler is post version 1.0 and quite stable. I think it's about 10 years in the making. He has even a longer history with developing compilers, as the author of Zortech/Symantec C++. In fact, most languages have been the works of a single individual. The Java and .NET stuff are pretty much exceptions to that.

His compiler already performs basically as good as C++ (sometimes better, sometimes worse - depends on the benchmark), so I really don't think it's a good reason to avoid the language altogether.

I really suggest that curious people read the D overview, as it's quite informative and honest.

Oh, and Python fans might want to look at pyd. It lets you run compiled D modules from within Python.

Kitty Cat
Member #2,815
October 2002
avatar

I'll wait 'til it can interface C++ code. My current work has been around COM stuff (*shudders*), and C++ makes implementing interfaces very slick. Without C++, I'd have to do it the C way, which is just ugly.

And am I the only one that doesn't like languages trying to hide pointers? I don't even like using non-const references because it gets confusing to know which variable changes would persist out of the function and which won't..

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

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

And am I the only one that doesn't like languages trying to hide pointers?

I hate half ass-ed languages. Either its hidden all the time, or never. Either its completely high level, or as low level a high level language can get (ie: C). I find C++ tries to be too much, and fails miserably.

Heh, along with the C++ operation, C++ evaluates to C, making it a no-op as far as the useable return result is concerned. It tries to be more, but didn't quite make it ;)

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

CGamesPlay
Member #2,559
July 2002
avatar

My new language, ++C, won't have that problem.

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

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

Sevalecan
Member #4,686
June 2004
avatar

Quote:

And am I the only one that doesn't like languages trying to hide pointers? I don't even like using non-const references because it gets confusing to know which variable changes would persist out of the function and which won't..

No, I'm with you.

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.

bamccaig
Member #7,536
July 2006
avatar

KittyCat said:

And am I the only one that doesn't like languages trying to hide pointers? I don't even like using non-const references because it gets confusing to know which variable changes would persist out of the function and which won't..

Sevalecan Dragon said:

No, I'm with you.

Me too...

Samuel Henderson
Member #3,757
August 2003
avatar

KittyCat said:

And am I the only one that doesn't like languages trying to hide pointers? I don't even like using non-const references because it gets confusing to know which variable changes would persist out of the function and which won't..

Sevalecan Dragon said:

No, I'm with you.

bamccaig said:

Me too...

Me as well.

=================================================
Paul whoknows: Why is this thread still open?
Onewing: Because it is a pthread: a thread for me to pee on.

Myrdos
Member #1,772
December 2001

Quote:

His compiler already performs basically as good as C++ (sometimes better, sometimes worse - depends on the benchmark), so I really don't think it's a good reason to avoid the language altogether.

Minor problem: I develop Linux software. Using a proprietary compiler will reduce the popularity of my programs.

Major problem: My computers are 64 bit.

__________________________________________________

Matthew Leverton
Supreme Loser
January 1999
avatar

64-bit support is obviously a valid issue, but it being "proprietary" is just an excuse.

Quote:

The front end for D is open source, and the source comes with the compiler. The runtime library is completely open source. David Friedman has integrated the D frontend with GCC to create gdc, a completely open source implementation of D.

Honestly, I don't care if someone is too stuck up to download a free, open-source compiler because it's not part of their precious GPL commune. Those aren't the type of people likely to buy software anyway, so it's not like your commercial success will be hampered.

Quote:

And am I the only one that doesn't like languages trying to hide pointers? I don't even like using non-const references because it gets confusing to know which variable changes would persist out of the function and which won't..

I think that is spoken like a true C/C++ fan. I don't mean that in a bad way, but using a certain method for a while makes you start thinking it's the best way. If C++ and D both came out yesterday, I think basically everybody would choose D. However, now that you've used C/C++ for a while, you become partial to it and anything D does differently seems wrong.

Personally, I think that D's system is far less confusing than C++'s. It works the way I expect it to, without all the ugly pointer syntax. (In fact, even C++ tries to hide away pointers ... that's what references do.) But D's system wasn't as big of a shock to me, coming from language (like PHP) that automatically handle pointers as well. Obviously if the language does a poor, inconsistent job with replacing pointers, then it's something to be concerned about.

Edit: An example to show how D handles it:

1import std.stdio;
2 
3void setNullString(char[] s)
4{
5 s = null;
6}
7 
8void setNullInt(int i)
9{
10 i = 0;
11}
12 
13void setNullObj(Foo f)
14{
15 f = null;
16}
17 
18class Foo
19{
20 public char[] value;
21 
22 this(char[] value)
23 {
24 this.value = value;
25 }
26 
27 char[] toString()
28 {
29 return value;
30 }
31}
32 
33 
34int main()
35{
36 int x = 1;
37 char[] y = "Hello, World";
38 Foo z = new Foo("Foo");
39 
40 setNullInt(x);
41 setNullString(y);
42 setNullObj(z);
43 
44 writefln("%s %s %s", x, y, z);
45 
46 return 0;
47}

That would print "1 Hello, World Foo".

If I wanted the code to "work," I'd have to explicitly use the ref keyword, similar to C++'s & symbol.

However, dynamic arrays and objects are always pointers. That behavior is constant, so you never "don't know." For instance:

1import std.stdio;
2 
3// using Foo class from before
4 
5void changeFoo(Foo f, char[] newValue)
6{
7 // f is a pointer, so this will change the value
8 f.value = newValue;
9}
10 
11int main()
12{
13 Foo z = new Foo("Foo");
14 
15 changeFoo(z, "Test");
16 
17 writefln("%s", z);
18 
19 return 0;
20}

The above would output "Test", because all objects are pointers. If you really wanted to pass a copy of an object you have to explicitly do so on either end using the 'dup' property.

changeFoo(z.dup, "Test"); // wouldn't change z

Billybob
Member #3,136
January 2003

Quote:

Quote:

And am I the only one that doesn't like languages trying to hide pointers? I don't even like using non-const references because it gets confusing to know which variable changes would persist out of the function and which won't..

Yes, I'm against you.

Fixed.

bamccaig
Member #7,536
July 2006
avatar

Matthew Leverton said:

  char[] y = "Hello, World";
  Foo z = new Foo("Foo");

Matthew Leverton said:

  setNullInt(x);
  setNullString(y);
  setNullObj(z);

Matthew Leverton said:

That would print "1 Hello, World Foo".

Matthew Leverton said:

However, dynamic arrays and objects are always pointers. That behavior is constant, so you never "don't know."

:-/ I think I'm confused... ???

To be honest, the only thing I really have against D is the name... :-/ If I'm gonna start using it the language needs to be renamed... :-/:'(

Thomas Fjellstrom
Member #476
June 2000
avatar

Pointers work the same in D as in C, its passed in by value, so any setting of the pointer in the function sets a local one, but if you dereference the pointer (foo[1] = bar), you then set the contents.

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

Myrdos
Member #1,772
December 2001

ML said:

Honestly, I don't care if someone is too stuck up to download a free, open-source compiler because it's not part of their precious GPL commune.

Digital Mars license said:

The Software is copyrighted and comes with a single user license, and may not be redistributed.

What I meant was that Linux distributions won't be able to package the D compiler and include it in their repository. Therefore, they won't package any software written with D.

Compiling software from source in binary-package distributions can be hit or miss because of missing dependencies. For example, if Allegro were written in D, the average user wouldn't be able to install it in Ubuntu*. You'd have to make a list of all the dependencies and tweaks for every Linux distribution separately.

However, this issue only affects Linux users. Windows and Mac folk can get the executable.

__________________________________________________

bamccaig
Member #7,536
July 2006
avatar

Thomas Fjellstrom said:

Pointers work the same in D as in C, its passed in by value, so any setting of the pointer in the function sets a local one, but if you dereference the pointer (foo[1] = bar), you then set the contents.

I see... :-/ For a language with no actual pointer syntax I would think it would be pass by reference and not address. :-/ That sounds less than clean for strings (char[]) then... :-/

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

I see... :-/ That sounds less than clean for strings (char[]) then... :-/

Reading the docs would help you from looking like a fool, but because I know you won't, here you go:

string[] = someotherstring;

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

bamccaig
Member #7,536
July 2006
avatar

As I said earlier, I don't have time to learn about D right now. That's what a thread like this is for. For those that are reading up on it to update the rest of us who are busy with other things.

Kitty Cat
Member #2,815
October 2002
avatar

Quote:

If I wanted the code to "work," I'd have to explicitly use the ref keyword, similar to C++'s & symbol.

And as I mention, using non-const &/references makes it confusing too, since there's no indication in the function that you're doing "more permanent" changes. With a proper pointer you'd have to do
*foo = blah;
which makes it clear that you're modifying memory from somehwhere else and not that specific variable (assuming no operator overloads). If you do
foo = blah;
are you modifying a variable specific to the function, or are you also modifying the variable that the calling function passed? In C++ you'd have & at the declaration to tell you (which is still a bit too transparent for my tastes), where as in D, you have the ref keyword, but also the implicitness of objects (are typedef'd enums and basic types also implicitly references? if not, then that's more room for confusion).

Quote:

However, dynamic arrays and objects are always pointers. That behavior is constant, so you never "don't know." For instance:

If you have a large-ish function where you can't easilly check the variable's declaration, it's easy to miss or not know. Having to be explicit about dereferencing makes it easier to see that you're modifying memory from elsewhere.

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

Matthew Leverton
Supreme Loser
January 1999
avatar

Quote:

For a language with no actual pointer syntax

It does have pointers, it's just that you don't really ever need to use it. For instance:

1import std.stdio;
2 
3int main()
4{
5 int x = 1;
6 int* px = &x;
7 int[] y;
8 int[]* py = &y;
9
10 *px = 2;
11
12 (*py).length = 1; // resize the dynamic array
13 *py[0] = 42;
14
15 writefln("%d, %d", x, y[0]);
16
17 return 0;
18}

That prints "2, 42" as you would expect.

Remember that it can interface directly with C, so it has to let you do stuff like that.

Quote:

Having to be explicit about dereferencing makes it easier...

I disagree, but at some level, it's just personal taste. I'd rather know I always have to do object.bar; I can very easily look at the function "prototype" to determine what's going to happen.



Go to: