Effective D : best practices and design patterns
Asked Answered
G

2

12

A really interesting conference was given about D-Specific Design Patterns and in the D community, some people thought it could be a starting point for a book dealing about effective coding techniques. Others argue that it was too early as not many people have lots of experience, the author(s) of such a book will have some biased/personal appreciation about the notion of effectiveness. SO is a more interactive media (with its limitations). So, waiting for 'Effective D' to come out, it will be great if we could share some (killing) advices/techniques/patterns to make D code look nicer. I think it will be clearer if an answer:

  • expose a unique technique
  • is essentially a piece of commented code
  • (if it is too large) is just a link to the code (public gist...)
Gleich answered 15/6, 2013 at 19:40 Comment(5)
Probably should be done as "community wiki" question instead.Lengthwise
Is there a way to "convert" the question ?Gleich
Maybe someone with high enough rep can do it.Lengthwise
requests for lists are considered off-topic hereAchlamydeous
Hopefully if it's a wiki it isn't...it seems to me that a lot of successful questions produced a list of rated answers (even it isn't so clearly express but then if it is a semantic problem maybe I could fix this). Furthermore, I'm sure the d tag constitute such a remote part of SO that the moral keepers who only gets interested at this meta-level won't be so numerous.vGleich
A
5

I have a few D Tip posts on my blog (1 2 3).

Here's one: Testing With TypeTuple


When testing a function, it’s usually a good idea to test it with a range of different inputs. To do this, you can easily use a for loop over an array of input values, but what if your input is a type, as it often is with template code?

The D Programming Language allows you to iterate over a TypeTuple, so all you need to do is declare a tuple of all the types you want to test, and iterate over them in the normal way:

import std.typetuple;
alias TypeTuple!(int, long, double) Types;
foreach (T; Types)
    test!T();

You might wonder what this compiles to. After all, the body of the loop varies with T, so the generated code must also vary on each iteration. How does the compiler handle this?

The answer is that the loop is completely unrolled. The code above is literally the same as:

test!int();
test!long();
test!double();

For this reason, you might want to keep an eye on the size of your TypeTuples, to avoid code bloat.

Arta answered 16/6, 2013 at 19:34 Comment(1)
Great tips ! Especially the one about the interest of using ranges in generic code through the example of the subtle string type.Gleich
G
1

there is a collection of d-idioms / patterns to be found under https://p0nce.github.io/d-idioms/

Gunsel answered 10/7, 2016 at 16:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.