Idiomatic D

Kelet kelethunter at gmail.com
Fri Jan 10 15:16:09 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?

I'm pretty new to D myself, but I always strive to write 
reasonably idiomatic code. So here's more or less what I've 
picked up:

- Use type inference (auto) when possible
- Use variable, function, and parameter qualifiers[1]
- Use compile time function execution[2]
- Constrain templates as much as possible
- foreach or higher-order functions are preferred over for/while 
loops[3]
- Do not overuse OOP[4]
- Use uniform function call syntax when it makes sense to chain 
calls or increase readability
- Know `alias this` and operator overloads
- Put unit tests under each function if you choose to make them
- Use Ddoc-style documentation
- Many of the examples on Rosetta Code are well-written 
http://rosettacode.org/wiki/Category:D I think bearophile did 
many of them.

[1]:
Some Qualifiers to note -
Function:
   - auto: infer return type
   - ref: returns a reference, not a value
   - inout: Deduce immutability (mutable, const, immutability) by 
parameters
   - pure: does not modify or use global state or impure functions
   - nothrow: No possibility to throw an exception
   - @safe: No operation may corrupt memory
   - @trusted – consider function safe, even if it cannot be 
verified so
   - @system – default, all @safe and @trusted functions are also 
@system
Variable:
   - const: this reference to the variable is unchangeable
     - const is nice in function parameters because it can accept 
mutable, const,
       and immutable parameters
   - immutable: all references to the variable are unchangeable
   - shared: requires that variable is sharable between threads
Parameter/Argument:
   - scope: no reference to variable can be leaked outside of 
function
   - in: equivalent to const scope
   - out: like ref, but gives default value, can be used for 
multiple returns
   - inout: see function qualifier definition
   - lazy: calculate only on usage (can duplicate calculations)

Mostly, I find myself trying to use `pure @safe nothrow` in 
functions, `immutable` in variables, and `in` in parameters as 
much as possible without modifying semantics too much.

[2]: See last example here 
http://en.wikipedia.org/wiki/Compile_time_function_execution

[3]:
for(int i = 0; i < 20; i++) =>
foreach(immutable i = 0; 0..20) OR
Use some higher-order function from std.algorithm or whatnot 
instead

[4]:
D is not Java. Modules can have their own constructors and 
private variables and such.

Regards,
Kelet


More information about the Digitalmars-d mailing list