View source code
Display the source code in std/typecons.d from which this page was generated on
github.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Page wiki
View or edit the community-maintained wiki page associated with this page.
Struct std.typecons.Typedef
Typedef
allows the creation of a unique type which is
based on an existing type. Unlike the alias
feature,
Typedef
ensures the two types are not considered as equals.
Example
alias MyInt = Typedef!int; static void takeInt(int) { } static void takeMyInt(MyInt) { } int i; takeInt(i); // ok takeMyInt(i); // fails MyInt myInt; takeInt(myInt); // fails takeMyInt(myInt); // ok
Parameters
Name | Description |
---|---|
init | Optional initial value for the new type. For example:
----
alias MyInt = Typedef !(int, 10);
MyInt myInt;
assert(myInt == 10); // default-initialized to 10
----
|
cookie | Optional, used to create multiple unique types which are
based on the same origin type T . For example:
----
alias TypeInt1 = Typedef !int;
alias TypeInt2 = Typedef !int;
// The two Typedefs are the same type.
static assert(is(TypeInt1 == TypeInt2));
alias MoneyEuros = Typedef !(float, float.init, "euros");
alias MoneyDollars = Typedef !(float, float.init, "dollars");
// The two Typedefs are not_ the same type.
static assert(!is(MoneyEuros == MoneyDollars));
---- |
Note
If a library routine cannot handle the Typedef
type,
you can use the
template to extract the
type which the TypedefType
Typedef
wraps.
Authors
Andrei Alexandrescu, Bartosz Milewski, Don Clugston, Shin Fujishiro, Kenji Hara