Bug 9166 – std.typecons.Nullable and NullableRef don't work with a not mutable type
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2012-12-16T18:22:00Z
Last change time
2014-06-06T18:15:02Z
Keywords
rejects-valid
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2012-12-16T18:22:44Z
import std.typecons: Nullable;
void main() {
auto n = Nullable!(immutable int)(5);
}
DMD 2.061alpha gives:
...\dmd2\src\phobos\std\typecons.d(1170): Error: can only initialize const member _value inside constructor
test.d(3): Error: template instance std.typecons.Nullable!(immutable(int)) error instantiating
The problem is in this part of std.typecons.Nullable:
/**
Assigns $(D value) to the internally-held state. If the assignment
succeeds, $(D this) becomes non-null.
*/
void opAssign()(T value)
{
_value = value;
_isNull = false;
}
If I put it inside a "static if" it seems to work:
static if (isMutable!T) {
/**
Assigns $(D value) to the internally-held state. If the assignment
succeeds, $(D this) becomes non-null.
*/
void opAssign()(T value)
{
_value = value;
_isNull = false;
}
}
Comment #1 by bearophile_hugs — 2012-12-16T18:47:03Z
See also Issue 9164
Comment #2 by bearophile_hugs — 2012-12-26T15:58:54Z
The same happens with NullableRef:
import std.typecons: NullableRef;
alias NII = NullableRef!(immutable int);
void main() {}
...\dmd2\src\phobos\std\typecons.d(1451): Error: cannot modify immutable expression *this._value
test.d(2): Error: template instance std.typecons.NullableRef!(immutable(int)) error instantiating