`examplevalues` property

Andrea Fontana via Digitalmars-d digitalmars-d at puremagic.com
Mon Aug 3 06:13:54 PDT 2015


On Monday, 3 August 2015 at 12:13:15 UTC, HaraldZealot wrote:
> I found myself in situation that were good that all types 
> support `.examplevalues` property in unittest version. This 
> property will return array of predefined values for specified 
> type (we can even have some convention like `examplevalues[0]` 
> is `init`, `examplevalues[1]` is `min` (for numerical type) an 
> so on). If custom types doesn't redefine this property the 
> array consist only from `init`.
>
> The use case for this: templated struct or class with 
> container-like semantics and internal unittest for method of 
> such class.
>
> Thoughts?

Why don't you use templates? Something like:

enum ValueType
{
	Init,
	Min,
	Max
}

auto exampleValues(T)()
{
	T[ValueType] retVal;

	retVal[ValueType.Init] = T.init;
	static if (__traits(compiles, T.min)) retVal[ValueType.Min] = 
T.min;
	static if (__traits(compiles, T.max)) retVal[ValueType.Max] = 
T.max;

	return retVal;
}

exampleValues!int.writeln;
exampleValues!string.writeln;



More information about the Digitalmars-d mailing list