Difference between revisions of "Voldemort types"

From D Wiki
Jump to: navigation, search
(stub)
 
(expand)
Line 1: Line 1:
 
In D, a '''Voldemort type''' is a type that cannot be named outside of the scope it's declared in, but code outside the scope can still use this type by taking advantage of D's static type inference.
 
In D, a '''Voldemort type''' is a type that cannot be named outside of the scope it's declared in, but code outside the scope can still use this type by taking advantage of D's static type inference.
 +
 +
==Usage==
  
 
For example:
 
For example:
Line 14: Line 16:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
Here is how the Voldemort type can be used:
 +
 +
<syntaxhighlight lang=D>
 +
auto voldemort = createVoldemortType();
 +
writeln(voldemort.nameableMember);  // prints 123
 +
</syntaxhighlight>
 +
 +
The type of the variable voldemort cannot be explicitly named here, because that name is only accessible inside the function createVoldemortType. However, we can still declare variables of that type by using D's static type inference.
 +
 +
==Purpose==
 +
 +
{{sectionstub}}
 +
 +
Voldemort types are widely used in [[Phobos]]'s range-based functions.
 +
 +
==See also==
 +
 +
* [[Initializing variables]]

Revision as of 18:14, 14 December 2012

In D, a Voldemort type is a type that cannot be named outside of the scope it's declared in, but code outside the scope can still use this type by taking advantage of D's static type inference.

Usage

For example:

// Note: the return type is auto, because we cannot actually name it outside the function!
auto createVoldemortType()
{
    struct TheUnnameable
    {
        int nameableMember;
    }
    return TheUnnameable(123);
}

Here is how the Voldemort type can be used:

auto voldemort = createVoldemortType();
writeln(voldemort.nameableMember);  // prints 123

The type of the variable voldemort cannot be explicitly named here, because that name is only accessible inside the function createVoldemortType. However, we can still declare variables of that type by using D's static type inference.

Purpose

This section is a stub. Please help the wiki by adding more content here.

Voldemort types are widely used in Phobos's range-based functions.

See also