unittesting generic functions

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 14 12:23:15 PDT 2014


On Thursday, 14 August 2014 at 05:53:43 UTC, Meta wrote:
> On Thursday, 14 August 2014 at 01:10:54 UTC, Andrei 
> Alexandrescu wrote:
>> Destroy https://issues.dlang.org/show_bug.cgi?id=13291?
>>
>> Andrei
>
> It's worth noting that you can currently do this:
>
> template fun(T)
> {
> 	int fun(T val)
> 	{
> 		static if(is(T == string) || is(T == float))
> 		{
> 			return 0;
> 		}
> 		else
> 		{
> 			return 1;
> 		}
> 	}
> 	
> 	unittest
> 	{
> 		assert(fun(T.init) == 0);
> 	}
> }
>
> void main()
> {
> 	fun("test"); //Ok
> 	fun(3.0f);   //Ok
> 	fun(1);      //AssertError thrown
> }

Yeah. This proposal adds a shortcut to do this sort of thing, but 
it doesn't actually add new functionality. And in my experience, 
it's not even useful functionality. It's just too hard to be able 
to generic types, because construction isn't generic. _Some_ 
things can be tested generically (particularly if all you need is 
the init value for a type), and it's occasionally useful, but in 
most cases, it isn't. So, I don't think that it really hurts to 
add this feature, but I don't think that it really adds much 
either.

- Jonathan M Davis


More information about the Digitalmars-d mailing list