Channels ▼

Walter Bright

Dr. Dobb's Bloggers

Voldemort Types In D

May 07, 2012

Sometimes, the confluence of existing features can yield unexpected surprises. While I'd like to think that we designed Voldemort Types into the D programming language, the reality is that they were discovered by Andrei Alexandrescu. What are Voldemort Types? Read on.

First, a bit of background.

An InputRange in D is a type with the following members:

front - get the first element of the range
popFront - remove the first element of the range
empty - are there more elements in the range?

which form the basis of iteration. Just for fun, let's design an InputRange that will return a sequence of random numbers, forever. (We can also call this a generator.)

It can look like this (not a very good random number generator, but it'll do for the moment):

module rnd;

struct RandomNumberGenerator {

    this(uint seed) {
        next = seed;
    popFront();  // get it going

    @property int front() {
    return ((next / 0x10000) * next) >> 16;

    void popFront() {
    next = next * 1103515245 + 12345;

    @property bool empty() { return false; }

    uint next;

and a function that'll return it:

  RandomNumberGenerator generator(uint seed) {
    return RandomNumberGenerator(seed);

and a lovely program that will print out 10 such numbers:

  import std.stdio;
  import rnd;

  void main() {
    int count;
    foreach (n; generator(5)) {
        if (++count == 10)

and the result is:


That's where one would normally stop. But there's just something annoying about it. All I really care about is the rnd.generator function, but yet there's this type RandomNumberGenerator sitting out there by itself. It just looks like a failure of encapsulation, as it is leaking outside of my generator abstraction.

I could mark it with the private attribute and modules other than rnd won't be able to access it. But it's still there, outside the scope of where it belongs, and other module members can still access it, private or not (in D, private declarations are not hidden from other declarations within the same module). Besides, I want it to be so clean it squeaks.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.