Better assert without changing built-in assert

2011-02-16 Thread Jens Mueller
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


Re: Better assert without changing built-in assert

2011-02-16 Thread Jim
Jens Mueller Wrote:

 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


Or like this:


import std.conv;

string cat(T...)(T ts)
{
string s;
foreach(t; ts)
 s ~= to!string(t) ~  ;
return s;
}


unittest
{
...
assert(a==b, cat(a,b));
}


Re: Better assert without changing built-in assert

2011-02-16 Thread Andrei Alexandrescu

On 2/16/11 5:04 AM, Jim wrote:

Jens Mueller Wrote:


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



Or like this:


import std.conv;

string cat(T...)(T ts)
{
string s;
foreach(t; ts)
 s ~= to!string(t) ~  ;
return s;
}


unittest
{
...
assert(a==b, cat(a,b));
}


There's already text in std.conv with the same semantics.

{
   assert(a==b, text(a,b));
}


Andrei


Re: Better assert without changing built-in assert

2011-02-16 Thread Jim
Jens Mueller Wrote:

 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


Or like this:


import std.conv;

string cat(T...)(T ts)
{
string s;
foreach(t; ts)
 s ~= to!string(t) ~  ;
return s;
}


unittest
{
...
assert(a==b, cat(a,b));
}