www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Some testing?

reply bearophile <bearophileHUGS lycos.com> writes:
This small D2 program compiles with no errors:

T foo(T)(T x) {}
void main() {}

But there is no way it can compile, regardless of the type T, because foo()()
lacks a return statement. 
So is it possible for the D compiler to perform some sanity tests for the
template functions too, to catch a bugs like this one?

Bye,
bearophile
Jul 14 2010
next sibling parent Jonathan M Davis <jmdavisprog gmail.com> writes:
On Wednesday, July 14, 2010 11:55:57 bearophile wrote:
 This small D2 program compiles with no errors:
 
 T foo(T)(T x) {}
 void main() {}
 
 But there is no way it can compile, regardless of the type T, because
 foo()() lacks a return statement. So is it possible for the D compiler to
 perform some sanity tests for the template functions too, to catch a bugs
 like this one?
 
 Bye,
 bearophile
You get an error if you use foo. My guess would be that foo effectively doesn't exist until you instantiate it, so there's not really a function to give you an error on. - Jonathan M Davis
Jul 14 2010
prev sibling next sibling parent reply Mafi <mafi example.org> writes:
Am 14.07.2010 20:55, schrieb bearophile:
 This small D2 program compiles with no errors:

 T foo(T)(T x) {}
 void main() {}

 But there is no way it can compile, regardless of the type T, because foo()()
lacks a return statement.
 So is it possible for the D compiler to perform some sanity tests for the
template functions too, to catch a bugs like this one?
Imagine T was void. Then no return was ok. It wouldn't (shouldn't) compile anyways because a parameter of type void is (should be) invalid. However to enforce the compiler to see that T can't be void is a bit to complicated, I think. Mafi
Jul 14 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Mafi:
 Imagine T was void. Then no return was ok. It wouldn't (shouldn't) 
 compile anyways because a parameter of type void is (should be) invalid. 
 However to enforce the compiler to see that T can't be void is a bit to 
 complicated, I think.
You are right, this different version runs: T foo(T)() {} void main() { foo!void(); } Thanks for all the answers, to you, Don and Jonathan. A question like this was probably more at home in the IRC channel. Bye, bearophile
Jul 14 2010
prev sibling parent Don <nospam nospam.com> writes:
bearophile wrote:
 This small D2 program compiles with no errors:
 
 T foo(T)(T x) {}
 void main() {}
 
 But there is no way it can compile, regardless of the type T, because foo()()
lacks a return statement. 
 So is it possible for the D compiler to perform some sanity tests for the
template functions too, to catch a bugs like this one?
 
 Bye,
 bearophile
Yes, it'd be possible for such simple cases, but would it be worthwhile? Since an error message will occur for ANY instantiation of foo, the only errors that would catch are when there's absolutely no testing or use of foo. In which case you have much bigger problems. And unfortunately it doesn't scale to more complex situations where it might actually help.
Jul 14 2010