View source code
Display the source code in std/typecons.d from which this
page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a
Bugzilla issue.
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.
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.
struct Typedef(T, T init = T .init, string cookie = null)
;
Methods
Name | Description |
---|---|
toString
|
Convert wrapped value to a human readable string |
Parameters
Name | Description |
---|---|
init | Optional initial value for the new type. |
cookie | Optional, used to create multiple unique types which are
based on the same origin type T |
Note
If a library routine cannot handle the Typedef type,
you can use the TypedefType
template to extract the
type which the Typedef wraps.
Example
alias MyInt = Typedef!int;
MyInt foo = 10;
foo++;
writeln(foo); // 11
Example
custom initialization values
alias MyIntInit = Typedef!(int, 42);
static assert(is(TypedefType!MyIntInit == int));
static assert(MyIntInit() == 42);
Example
Typedef creates a new type
alias MyInt = Typedef!int;
static void takeInt(int) {}
static void takeMyInt(MyInt) {}
int i;
takeInt(i); // ok
static assert(!__traits(compiles, takeMyInt(i)));
MyInt myInt;
static assert(!__traits(compiles, takeInt(myInt)));
takeMyInt(myInt); // ok
Example
Use the optional cookie
argument to create different types of the same base type
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));
Authors
Andrei Alexandrescu, Bartosz Milewski, Don Clugston, Shin Fujishiro, Kenji Hara
License
Copyright © 1999-2022 by the D Language Foundation | Page generated by ddox.