D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4471 - rebindable() helper function
Summary: rebindable() helper function
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2010-07-16 05:21 UTC by bearophile_hugs
Modified: 2010-08-15 10:05 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-07-16 05:21:15 UTC
A helper function for Rebindable!() can help remove some noise from the code, this is a first try:


import std.typecons, std.traits;

template isImmutable(T) {
    enum bool isImmutable = is(const(T) == T) || is(immutable(T) == T);
}

Rebindable!T rebindable(T)(T obj)
  if ((is(T == class) || is(T == interface) || isArray!T) && isImmutable!T) {
    return Rebindable!T(obj);
}

// usage example
import std.stdio;
class Foo {}
void main() {
    auto a = rebindable(new immutable(Foo));
    assert(a.sizeof == 4);
    a = new immutable(Foo);
}


I don't know if isImmutable!() works well in all cases, I have not tested it much. But a template like isImmutable can be useful in Phobos.
Comment 1 bearophile_hugs 2010-08-15 10:05:41 UTC
Fixed in:
http://dsource.org/projects/phobos/changeset/1849