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.
							
						Module std.typecons
This module implements a variety of type constructors, i.e., templates that allow construction of new, useful general-purpose types.
| Category | Functions | 
|---|---|
| Tuple | isTupleTupletuplereverse | 
| Flags | BitFlagsisBitFlagEnumFlagNoYes | 
| Memory allocation | RefCountedrefCountedRefCountedAutoInitializescopedUnique | 
| Code generation | AutoImplementBlackHolegenerateAssertTrapgenerateEmptyFunctionWhiteHole | 
| Nullable | NullablenullableNullableRefnullableRef | 
| Proxies | ProxyrebindableRebindableReplaceTypeunwrapwrap | 
| Types | alignForSizeTernaryTypedefTypedefTypeUnqualRef | 
Example
// value tuples
alias Coord = Tuple!(int, "x", int, "y", int, "z");
Coord c;
c[1] = 1;       // access by index
cFunctions
| Name | Description | 
|---|---|
| 
									alignForSize()
								 | Order the provided members to minimize size while preserving alignment. Alignment is not always optimal for 80-bit reals, nor for structs declared as align(1). | 
| 
									nullable(t)
								 | Defines a value paired with a distinctive "null" state that denotes
the absence of a value. If default constructed, a Nullable!Tobject starts in the null state. Assigning it renders it
non-null. Callingnullifycan nullify it again. | 
| 
									nullable(t)
								 | Just like Nullable!T, except that the null state is defined as a
particular value. For example,Nullable!(uint, uintis anuintthat sets aside the valueuintto denote a null
state.Nullable!(T, nullValue)is more storage-efficient thanNullable!Tbecause it does not need to store an extrabool. | 
| 
									nullableRef(t)
								 | Just like Nullable!T, except that the object refers to a value
sitting elsewhere in memory. This makes assignments overwrite the
initially assigned value. InternallyNullableRef!Tonly stores a
pointer toT(i.e.,Nullable!T). | 
| 
									rebindable(obj)
								 | Convenience function for creating a Rebindableusing automatic type
inference. | 
| 
									rebindable(obj)
								 | This function simply returns the Rebindableobject passed in.  It's useful
in generic programming cases when a given object may be either a regularclassor aRebindable. | 
| 
									refCounted(val)
								 | Initializes a RefCountedwithval. The template parameterTofRefCountedis inferred fromval.
 This function can be used to move non-copyable values to the heap.
 It also disables theautoInitoption ofRefCounted. | 
| 
									reverse(t)
								 | Creates a copy of a Tuplewith its fields in reverse order. | 
Classes
| Name | Description | 
|---|---|
| 
									AutoImplement
								 | AutoImplementautomatically implements (by default) all abstract member
functions in the class or interfaceBasein specified way. | 
Structs
| Name | Description | 
|---|---|
| 
									BitFlags
								 | A typesafe structure for storing combinations of enum values. | 
| 
									No
								 | Convenience names that allow using e.g. Yesinstead ofFlag!"encryption"andNoinstead ofFlag!"encryption". | 
| 
									Nullable
								 | Defines a value paired with a distinctive "null" state that denotes
the absence of a value. If default constructed, a Nullable!Tobject starts in the null state. Assigning it renders it
non-null. Callingnullifycan nullify it again. | 
| 
									Nullable
								 | Just like Nullable!T, except that the null state is defined as a
particular value. For example,Nullable!(uint, uintis anuintthat sets aside the valueuintto denote a null
state.Nullable!(T, nullValue)is more storage-efficient thanNullable!Tbecause it does not need to store an extrabool. | 
| 
									NullableRef
								 | Just like Nullable!T, except that the object refers to a value
sitting elsewhere in memory. This makes assignments overwrite the
initially assigned value. InternallyNullableRef!Tonly stores a
pointer toT(i.e.,Nullable!T). | 
| 
									RefCounted
								 | Defines a reference-counted object containing a Tvalue as
payload. | 
| 
									Ternary
								 | Ternary type with three truth values: | 
| 
									Tuple
								 | Tuple of values, for example Tuple!(int, string)is a record that
stores anintand astring.Tuplecan be used to bundle
values together, notably when returning multiple values from a
function. Ifobjis aTuple, the individual members are
accessible with the syntaxobj[0]for the first field,obj[1]for the second, and so on. | 
| 
									Typedef
								 | Typedef allows the creation of a unique type which is
based on an existing type. Unlike the aliasfeature,
Typedef ensures the two types are not considered as equals. | 
| 
									Unique
								 | Encapsulates unique ownership of a resource. | 
| 
									UnqualRef
								 | Similar to Rebindable!(T)but strips all qualifiers from the reference as
    opposed to just constness / immutability. Primary intended use case is with
    shared (having thread-local reference to shared class data) | 
| 
									Yes
								 | Convenience names that allow using e.g. Yesinstead ofFlag!"encryption"andNoinstead ofFlag!"encryption". | 
Enums
| Name | Description | 
|---|---|
| 
									Flag
								 | Defines a simple, self-documenting yes/no flag. This makes it easy for
APIs to define functions accepting flags without resorting to bool, which is opaque in calls, and without needing to define an
enumerated type separately. UsingFlag!"Name"instead ofboolmakes the flag's meaning visible in calls. Each yes/no flag has
its own type, which makes confusions and mix-ups impossible. | 
| 
									RefCountedAutoInitialize
								 | Options regarding auto-initialization of a RefCountedobject (see
the definition ofRefCountedbelow). | 
Templates
| Name | Description | 
|---|---|
| 
									apply
								 | Unpacks the content of a Nullable, performs an operation and packs it again. Does nothing if isNull. | 
| 
									Proxy
								 | Creates a proxy for the value athat will forward all operations
    while disabling implicit conversions. The aliased itemamust be
    an lvalue. This is useful for creating a new type from the
    "base" type (though this is not a subtype-supertype
    relationship; the new type is not related to the old type in any way,
    by design). | 
| 
									scoped
								 | Allocates a classobject right inside the current scope,
therefore avoiding the overhead ofnew. This facility is unsafe;
it is the responsibility of the user to not escape a reference to the
object outside the scope. | 
| 
									tuple
								 | Constructs a Tupleobject instantiated and initialized according to
    the given arguments. | 
| 
									unwrap
								 | Supports structural based typesafe conversion. | 
| 
									wrap
								 | Supports structural based typesafe conversion. | 
Manifest constants
| Name | Type | Description | 
|---|---|---|
| generateAssertTrap | Predefined how-policies for AutoImplement.  These templates are also used byBlackHoleandWhiteHole, respectively. | |
| generateEmptyFunction | Predefined how-policies for AutoImplement.  These templates are also used byBlackHoleandWhiteHole, respectively. | |
| isBitFlagEnum | Detect whether an enum is of integral type and has only "flag" values (i.e. values with a bit count of exactly 1). Additionally, a zero value is allowed for compatibility with enums including a "None" value. | |
| isTuple | Returns trueif and only ifTis an instance ofTuple. | 
Aliases
| Name | Type | Description | 
|---|---|---|
| BlackHole | AutoImplement!(Base,generateEmptyFunction,isAbstractFunction) | BlackHole!Baseis a subclass ofBasewhich automatically implements
all abstract member functions inBaseas do-nothing functions.  Each
auto-implemented function just returns the default value of the return type
without doing anything. | 
| Rebindable | const(ElementEncodingType!T)[] | Rebindable!(T)is a simple, efficient wrapper that behaves just
like an object of typeT, except that you can reassign it to
refer to another object. For completeness,Rebindable!(T)aliases
itself away toTifTis a non-const object type. | 
| ReplaceType | To | Replaces all occurrences of FromintoTo, in one or more typesT. For
example,ReplaceType!(int, uint, Tuple!(int, float)[string])yieldsTuple!(uint, float)[string]. The types in which replacement is performed
may be arbitrarily complex, including qualifiers, built-in type constructors
(pointers, arrays, associative arrays, functions, and delegates), and template
instantiations; replacement proceeds transitively through the type definition.
However, member types instructs orclasses are not replaced because there
are no ways to express the types resulting after replacement. | 
| TypedefType | Arg | Get the underlying type which a Typedefwraps.
IfTis not aTypedefit will alias itself toT. | 
| unwrap | unwrap!(Unqual!Target) | Supports structural based typesafe conversion. | 
| WhiteHole | AutoImplement!(Base,generateAssertTrap,isAbstractFunction) | WhiteHole!Baseis a subclass ofBasewhich automatically implements
all abstract member functions as functions that always fail. These functions
simply throw anErrorand never return.Whiteholeis useful for
trapping the use of class member functions that haven't been implemented. | 
| wrap | wrap!(staticMap!(Unqual,Targets)) | Supports structural based typesafe conversion. | 
Authors
Andrei Alexandrescu, Bartosz Milewski, Don Clugston, Shin Fujishiro, Kenji Hara
License
					Copyright © 1999-2022 by the D Language Foundation | Page generated by ddox.