Bug 4977 – cannot use nothrow or pure with Rebindable
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2010-10-02T17:39:00Z
Last change time
2015-06-09T05:14:39Z
Keywords
patch
Assigned to
nobody
Creator
issues.dlang
Comments
Comment #0 by issues.dlang — 2010-10-02T17:39:46Z
Rebindable does nothing about nothrow or pure. None of its functions are marked on nothrow or pure, making it rather difficult to use Rebindable with a type which has carefully been made to correctly use nothrow and/or pure.
The first thing would be to make opDot() nothrow and pure. I'm pretty sure that you can get away with that (though pure may not be possible until the next version of the compiler is released with the changes to pure that Don suggested).
opAssign() would probably be just as easy, though I'm not sure. Since we're dealing with copying classes, interfaces, or arrays, I think that you can just make opAssign nothrow with no problem (since you have no postblit constructor to worry about possibly throwing). On purity, I'm not so sure. It might work now, or it might require the changes Don suggested, but I think that it will work at that point.
Since the constructor uses opAssign(), it will probably be just as easy/difficult to change as opAssign will be.
In any case, as it stands, its rather difficult to use either nothrow or pure with Rebindable, which either severely limits where you can use Rebindable, or it severely limits the type that you put in Rebindable.
rebindable() will likely have to be changed appropriately as well.
Comment #1 by issues.dlang — 2010-10-02T18:25:53Z
It looks like opDot() doesn't work with const or immutable Rebindable!()'s. So, it probably needs a second version which is const (and possible a third for immutable) in order to work with them. Otherwise, const member functions don't work very well with member variables which are Rebindable!().
Comment #2 by issues.dlang — 2010-10-02T18:39:56Z
Here's a first attempt at a solution:
Rebindable(T) if (is(T == class) || is(T == interface) || isArray!(T))
{
static if (!is(T X == const(U), U) && !is(T X == immutable(U), U))
{
alias T Rebindable;
}
else static if (isArray!(T))
{
alias const(ElementType!(T))[] Rebindable;
}
else
{
struct Rebindable
{
private union
{
T original;
U stripped;
}
void opAssign(T another) nothrow
{
stripped = cast(U) another;
}
void opAssign(Rebindable another) nothrow
{
stripped = another.stripped;
}
static if (is(T == const U))
{
// safely assign immutable to const
void opAssign(Rebindable!(immutable U) another) nothrow
{
stripped = another.stripped;
}
}
this(T initializer) nothrow
{
opAssign(initializer);
}
@property T get() const pure nothrow
{
return original;
}
T opDot() pure nothrow
{
return original;
}
T opDot() const pure nothrow
{
return original;
}
}
}
}
With the current purity rules, I can't make opAssign() pure. It complains about assigning to const. I don't know whether or nat that will be fixed with the relaxed purity rules suggested by Don.
Comment #3 by issues.dlang — 2010-10-02T19:53:51Z
With the fix for bug # 3318, the new get property function now joins opDot() in needing a const version with both versions being nothrow.