Oh my gosh, the D.
Chris Katko

I'm writing more D code and I just... I can't stop finding things I love.

Like alias. It's like typedef or a macro replacement for a symbol but it's way better.

You can alias not just a TYPE, but also A VARIABLE. And not just a VARIABLE, but a structure's variable.

You can do:

alias x = system.document.file.is_ready;

Which allows you to do something I've never even thought of before. It feels like my brain just opened up a little.

Because I can now do this (and I'm going to extensively use it!):

1// Variable ABBREVIATIONS 2 3//actual code 4//member function of object_t (which has x, y, width, height) 5 6bool is_colliding_with(object_t obj) 7 { 8 alias x2 = obj.x; 9 alias y2 = obj.y; 10 alias width2 = obj.width; //clear note of dereferences 11 alias height2 = obj.height; 12 13 if( x > x2 + width2 - 1 || // Your eyes don't care about dereferences 14 y > y2 + height2 - 1 || // here, you care about the algorithm. 15 x2 > x + width - 1 || 16 y2 > y + height - 1) 17 { 18 return false; 19 } 20 21 return true; 22 }

I mean, look how concise that code is. And your eyes notice the patterns of what's going on, instead of getting bottled down in large dereferencing chains.

And yes, I get we "should" be able to notice them, but this makes it much clearer. And clearer reduces human errors.

I also like D's explicit member override operator "override." You can't override a base class's members without explicitly adding that keyword to the overriding function. That's another simple language design feature that prevents further errors.


Then again you don't really need an alias for that...

1bool is_colliding_with(object_t * obj) 2{ 3 int x = this->x, 4 y = this->y, 5 w = this->width, 6 h = this->height, 7 x2 = obj->x, 8 y2 = obj->y, 9 w2 = obj->width, 10 h2 = obj->height; 11 12 return !(x > x2 + w2 - 1 || 13 y > y2 + h2 - 1 || 14 x2 > x + w - 1 || 15 y2 > y + height - 1); 16}

Chris Katko

But here's the thing: Those are new variables.

alias is merely a symbol substitute. (like a statically check-able, define macro)

So you could use this even in tight loops.


I forgot! I had a second code snippet but I removed it. Another point here is that you can leave the function ARGUMENT names very long and descriptive, yet alias them to compact them.

void function(int very_descriptive_long_name_indicating_good_usage) 
alias v = very_descriptive_long_name_indicating_good_usage;

// do stuff with v

void main()
function(/*easier to understand function usage without also bloating the implementation of said function*/)

I'll have more real-world examples as I continue to use this pattern in my current project.


I use short variables to replace use of longer named dereferenced member variables all the time to make it more readable. Even though they're new variables, they're only used for read access to values, and probably optimize down to about the same code these aliases would.


Plus you only dereference once, no matter how many times you reference the variable. The compiler could probably do that for you, maybe, but I'm not sure it would.

Peter Hull

Like alias. It's like typedef or a macro replacement for a symbol but it's way better.

Isn't that just the same as a C++ reference?

Reference declaration

Declares a named variable as a reference, that is, an alias to an already-existing object or function.

I would like to take a good look at D one day but never seem to find the time. Bruce Perry is also a fan, I seem to remember.


Isn't that just a C macro ?

#define MY_BIG_ALIAS  whatever_you_want_even_a_function


Chris: except that in your example writing int v=myfuckinglongname would have been shorter by one two characters ^^


Missed the 's' FFS.

Chris Katko

Isn't that just a C macro ?

That's the thing. We all know macros are dangerous because they run at a separate processing step from the actual compiler. They're regex text modifiers. They're not statically checked. They're hard to debug. Tons of chances for side effects. No namespace or position respect. You could throw a macro literally anywhere in your code and it would attempt to match it, regardless of whether it's supposed to be there or not. Your simple macro above would literally rip out any matching variable names regardless of them being in your applicable function.

I'd rather have a one-word keyword tool that does exactly what it's supposed to, then re-invent the wheel on a more abstract processing layer. It allows compilers, text editors, static and dynamic analyzers, and everything else to rely on that specific, clear use-cases. Instead of trying to write code to imperfectly "detect it" by processing a macro/template and seeing if it resembles your case, and then doing the actual work you want to with it.

I'd rather have direct support for threads and shared data, than tons of macros littered about. I'd rather have support for array bounds checking then tons of macros littered about. (etc)

Isn't that just the same as a C++ reference?

You may be right... But it doesn't seem quite the same... references can be passed to functions. They're just implemented as pointers that can't be null or reassigned, right? (D supports references and pointers too.) D alias is simply a compile-time (scope checked!) rename/substitute of a symbol. As if you used a macro to change the variable name, but it works on the AST-level instead of on the raw-text level. So it can actually use the normal language constructs to ensure correctness.


It actually is possible to have a null reference in C++, but it's unlikely to occur unless you mean to or are mixing pointers and references. :)

#include <string>

int main(int argc, char * argv[])
    int * x = 0; // NULL pointer.
    int & y = *x; // OK!

    if(argc > 1 && std::string(argv[1]) == "assign")
        y = 5; // Crash!

    return 0;

Bruce Perry

I am a D fan, although I also recognise some of its shortcomings (last I checked) - in particular, the fact that you can't cleanly prevent use of the GC. I also have very little time for my own programming at the moment; hobby time gets spent on music. We use all sorts of languages at work but it's always a consequence of whatever engine or framework we've chosen, and sadly, D isn't one of them.

By the way, last I knew, D only supported references in function parameters or foreach iterator variables. If you wanted to declare one as a local like in C++, you had to make it a pointer, at which point, you strayed outside of the 'safe D' subset. I can't remember if there was a reason for this or whether it was an oversight.

Chris Katko

Bruce, you bring up a damn good point. When you're an adult it sucks because there's NEVER enough time to feed your hobbies.

I'm finally getting some health back so I can spend a couple hours a week coding for fun. I'm writing a ski free game, coded in D that I'm then going to convert to C++ for a real world, empirical speed test so you can see exactly what sections of D code are slower, but also, what sections BALLOON in size thanks to C++ boilerplate code.

On the otherhand, as much as I love writing music, I haven't touched FL Studio in over a year and I rarely pickup a guitar. I'll go for binges into a hobby or topic and I can't be focusing on programming and music at the same time. It's a different mode of thought for me that requires a ramp up and ramp down time to "get in the zone".

Same thing with many games. Games like akyrim, Witcher, Myst, all require you to get sucked into them for hours upon hours. You can't play 15 minutes of Skyrin, or Fallout New Vegas. 15 minutes is the time you'd spend walking from one town to another. But even if it wasn't, you know your heart won't be in it. You won't read some dialog and really care and experience the presentation had you gone all in with a binge.

Thread #616767. Printed from Allegro.cc