Idiomatic D

Brad Anderson eco at gnuk.net
Fri Jan 10 15:15:44 PST 2014


On Friday, 10 January 2014 at 22:52:36 UTC, NoUseForAName wrote:
> I want to implement a small program in D for a personal 
> comparison of various programming languages. Given that I am 
> already quite familiar with C/C++ (from K&R C to C++11) I could 
> probably start write working D code right away by just looking 
> up a few basic syntax/standard library things.
>
> However, I would like to write idiomatic D code, not "C++ in 
> D". Is there any guide for this? If not, I would like to say 
> that I think having one would be very useful.
>
> Assuming a lack of an extensive guide could someone please just 
> give me the cliff notes on how to D properly?

The first thing that comes to made is learn ranges and slices.

Ranges: http://ddili.org/ders/d.en/ranges.html
Slices: http://dlang.org/d-array-article.html

UFCS (pseudo-member) chains particularly with std.algorithm and 
std.range are becoming something of a strong idiom in D. 
Contrived example:

     void main()
     {
         import std.algorithm, std.uni;
         	
         auto result =  "one two three four five"
                       .splitter()
                       .map!toUpper
                       .filter!(a => a.canFind("E"));
         assert(result.equal(["ONE", "THREE", "FIVE"]));
    }

Users of D generally focus more on compile time polymorphism 
rather than runtime where possible. Nick Sabalausky wrote a 
rather good article about this: 
http://www.semitwist.com/articles/EfficientAndFlexible/MultiplePages/Page1/


More information about the Digitalmars-d mailing list