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.

std.typecons.rebindable - multiple declarations

Function rebindable

Convenience function for creating a Rebindable using automatic type inference.

Prototype

Rebindable!T rebindable(T)(
  T obj
)
if (is(T == class) || is(T == interface) || isDynamicArray!T);

Parameters

NameDescription
obj A reference to an object or interface, or an array slice to initialize the Rebindable with.

Returns

A newly constructed Rebindable initialized with the given reference.

Function rebindable

This function simply returns the Rebindable object passed in. It's useful in generic programming cases when a given object may be either a regular class or a Rebindable.

Prototype

Rebindable!T rebindable(T)(
  Rebindable!T obj
);

Parameters

NameDescription
obj An instance of Rebindable!T.

Returns

obj without any modification.

Template Rebindable

Rebindable!(T) is a simple, efficient wrapper that behaves just like an object of type T, except that you can reassign it to refer to another object. For completeness, Rebindable!(T) aliases itself away to T if T is a non-const object type. However, Rebindable!(T) does not compile if T is a non-class type.

You may want to use Rebindable when you want to have mutable storage referring to const objects, for example an array of references that must be sorted in place. Rebindable does not break the soundness of D's type system and does not incur any of the risks usually associated with cast.

Arguments

template Rebindable(T);

Parameters

NameDescription
T An object, interface, or array slice type.

Example

Regular const object references cannot be reassigned.

class Widget { int x; int y() const { return x; } }
const a = new Widget;
// Fine
a.y();
// error! can't modify const a
// a.x = 5;
// error! can't modify const a
// a = new Widget;

Example

However, Rebindable!(Widget) does allow reassignment, while otherwise behaving exactly like a const Widget.

class Widget { int x; int y() const { return x; } }
auto a = Rebindable!(const Widget)(new Widget);
// Fine
a.y();
// error! can't modify const a
// a.x = 5;
// Fine
a = new Widget;

Authors

Andrei Alexandrescu, Bartosz Milewski, Don Clugston, Shin Fujishiro, Kenji Hara

License

Boost License 1.0.

Comments