TypeFunction example: ImplictConvTargets

Daniel K dkm4i1 at gmail.com
Tue Oct 6 18:00:29 UTC 2020


On Monday, 5 October 2020 at 11:44:34 UTC, Stefan Koch wrote:
> Maybe you like the template version more?
> Let me know.

I love the premise. Like watching people in TV ads compare the 
knife of the competition with their own. Completely mutilating a 
loaf of bread with the competition.

What if we make an honest comparison?
I spent 15 minutes writing this:

template Tuple(T...)
{
     alias Tuple = T;
}

alias basic_types = Tuple!(bool, ubyte, char, byte, ushort, 
wchar, short, uint, dchar, int, ulong, long);

template ImplicitConvertible(T, Candidate, Candidates...)
{
	static if (is (T : Candidate))
		alias Include = Candidate;
	else
		alias Include = Tuple!();

	static if (Candidates.length > 0)
		alias ImplicitConvertible = Tuple!(Include, 
ImplicitConvertible!(T, Candidates));
	else
		alias ImplicitConvertible = Include;
}

template ImplicitConversionTargets(T)
{
	alias ImplicitConversionTargets = ImplicitConvertible!(T, 
basic_types);
}

pragma (msg, ImplicitConversionTargets!(long));

That's 16 lines of code. Heck it even compiles in D1 if only the 
alias declarations are written in the old style.

Personally I prefer using existing language features.

/Daniel K


More information about the Digitalmars-d mailing list