Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-enable std.typecons.Nullable and NullableRef #153

Merged
merged 3 commits into from Aug 14, 2011
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
130 changes: 116 additions & 14 deletions std/typecons.d
Expand Up @@ -1109,8 +1109,6 @@ unittest
assert(y == 5);
}

/+

/**
Defines a value paired with a distinctive "null" state that denotes
the absence of a valud value. If default constructed, a $(D
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're messing with this, you might as well fix the spelling in the description.

Expand Down Expand Up @@ -1145,7 +1143,7 @@ Constructor initializing $(D this) with $(D value).
/**
Returns $(D true) if and only if $(D this) is in the null state.
*/
bool isNull()
@property bool isNull() const
{
return _isNull;
}
Expand All @@ -1155,8 +1153,7 @@ Forces $(D this) to the null state.
*/
void nullify()
{
// destroy
//static if (is(typeof(_value.__dtor()))) _value.__dtor();
clear(_value);
_isNull = true;
}

Expand All @@ -1175,7 +1172,14 @@ Gets the value. Throws an exception if $(D this) is in the null
state. This function is also called for the implicit conversion to $(D
T).
*/
ref T get()
@property ref T get()
{
enforce(!isNull);
return _value;
}
//@@@BUG3748@@@: inout does not work properly.
// need to define a separate 'const' version
@property ref const(T) get() const
{
enforce(!isNull);
return _value;
Expand All @@ -1192,9 +1196,55 @@ unittest
{
Nullable!int a;
assert(a.isNull);
assertThrown(a.get);
a = 5;
assert(!a.isNull);
assert(a == 5);
//@@@BUG5188@@@
//assert(a != 3);
assert(a.get != 3);
a.nullify();
assert(a.isNull);
a = 3;
assert(a == 3);
a *= 6;
assert(a == 18);
a.nullify();
assertThrown(a += 2);
}
unittest
{
auto k = Nullable!int(74);
assert(k == 74);
k.nullify();
assert(k.isNull);
}
unittest
{
static int f(in Nullable!int x) {
return x.isNull ? 42 : x.get;
}
Nullable!int a;
assert(f(a) == 42);
a = 8;
assert(f(a) == 8);
a.nullify();
assert(f(a) == 42);
}
unittest
{
static struct S { int x; }
Nullable!S s;
assert(s.isNull);
s = S(6);
assert(s == S(6));
//@@@BUG5188@@@
//assert(s != S(0));
assert(s.get != S(0));
s.x = 9190;
assert(s.x == 9190);
s.nullify();
assertThrown(s.x = 9441);
}

/**
Expand All @@ -1219,7 +1269,7 @@ Constructor initializing $(D this) with $(D value).
/**
Returns $(D true) if and only if $(D this) is in the null state.
*/
bool isNull()
@property bool isNull() const
{
return _value == nullValue;
}
Expand All @@ -1246,7 +1296,13 @@ Gets the value. Throws an exception if $(D this) is in the null
state. This function is also called for the implicit conversion to $(D
T).
*/
ref T get()
@property ref T get()
{
enforce(!isNull);
return _value;
}
//@@@BUG3748@@@
@property ref const(T) get() const
{
enforce(!isNull);
return _value;
Expand All @@ -1263,9 +1319,30 @@ unittest
{
Nullable!(int, int.min) a;
assert(a.isNull);
assertThrown(a.get);
a = 5;
assert(!a.isNull);
assert(a == 5);
static assert(a.sizeof == int.sizeof);
}
unittest
{
auto a = Nullable!(int, int.min)(8);
assert(a == 8);
a.nullify();
assert(a.isNull);
}
unittest
{
static int f(in Nullable!(int, int.min) x) {
return x.isNull ? 42 : x.get;
}
Nullable!(int, int.min) a;
assert(f(a) == 42);
a = 8;
assert(f(a) == 8);
a.nullify();
assert(f(a) == 42);
}

/**
Expand Down Expand Up @@ -1297,7 +1374,7 @@ Binds the internal state to $(D value).
/**
Returns $(D true) if and only if $(D this) is in the null state.
*/
bool isNull()
@property bool isNull() const
{
return _value is null;
}
Expand All @@ -1324,7 +1401,13 @@ Gets the value. Throws an exception if $(D this) is in the null
state. This function is also called for the implicit conversion to $(D
T).
*/
ref T get()
@property ref T get()
{
enforce(!isNull);
return *_value;
}
//@@@BUG3748@@@
@property ref const(T) get() const
{
enforce(!isNull);
return *_value;
Expand All @@ -1339,17 +1422,36 @@ the null state.

unittest
{
int x = 5;
int x = 5, y = 7;
auto a = NullableRef!(int)(&x);
assert(!a.isNull);
assert(a == 5);
assert(x == 5);
a = 42;
assert(x == 42);
assert(!a.isNull);
assert(a == 42);
a.nullify();
assert(x == 42);
assert(a.isNull);
assertThrown(a.get);
assertThrown(a = 71);
a.bind(&y);
assert(a == 7);
y = 135;
assert(a == 135);
}
unittest
{
static int f(in NullableRef!int x) {
return x.isNull ? 42 : x.get;
}
int x = 5;
auto a = NullableRef!int(&x);
assert(f(a) == 5);
a.nullify();
assert(f(a) == 42);
}

+/


/**
$(D BlackHole!Base) is a subclass of $(D Base) which automatically implements
Expand Down