Better assert without changing built-in assert

Jens Mueller jens.k.mueller at gmx.de
Wed Feb 16 00:52:23 PST 2011


Hi,

I'm trying to improve the assertions. I tried the following
auto foo(bool var) {
	return tuple(var, "MyMessage");
}

void bar(bool var, string text) {
}

unittest {
	bar(foo(true).expand);
	//assert(foo(true).expand); // won't compile
}

void main() {}

$ dmd -unittest -run file.d
Commenting in the assert it fails with
"Error: expression
tuple(foo(true)._field_field_0,foo(true)._field_field_1) of type (bool,
string) does not have a boolean value"

assert behaves different when expanding tuples. Any explanations?
If one could do the above without writing the expand then it would
suggest a nice way of enhancing the build-in assert, I think.
If one cannot make it work without the .expand another option may be
to overload the built-in assert to handle tuples. This may be related to
http://d.puremagic.com/issues/show_bug.cgi?id=5547
Then one writes e.g.
assert(throws!MyException(expression))
or
assert(pred!("==")(a, b))
What do you think?

Jens


More information about the Digitalmars-d mailing list