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.

std.typecons.Rebindable/rebindable - multiple declarations

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.

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

Parameters

NameDescription
obj An instance of Rebindable!T.

Returns

obj without any modification.

Example

class C
{
    int payload;
    this(int p) { payload = p; }
}
const c = new C(1);

auto c2 = c.rebindable;
writeln(c2.payload); // 1
// passing Rebindable to rebindable
c2 = c2.rebindable;
writeln(c2.payload); // 1

Function rebindable

Convenience function for creating a Rebindable using automatic type inference.

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

Parameters

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

Returns

A newly constructed Rebindable initialized with the given reference.

Example

class C
{
    int payload;
    this(int p) { payload = p; }
}
const c = new C(1);

auto c2 = c.rebindable;
writeln(c2.payload); // 1
// passing Rebindable to rebindable
c2 = c2.rebindable;

c2 = new C(2);
writeln(c2.payload); // 2

const c3 = c2.get;
writeln(c3.payload); // 2

Alias 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.

alias Rebindable(T) = const(ElementEncodingType!T)[];

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.

Parameters

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

Example

Regular const object references cannot be reassigned.

class Widget { int x; int y() @safe 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 @safe { 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.