TypeFunction example: ImplictConvTargets
Adam D. Ruppe
destructionator at gmail.com
Mon Oct 5 21:20:15 UTC 2020
On Monday, 5 October 2020 at 11:44:34 UTC, Stefan Koch wrote:
> I leave it up to you to decide which version is more
> understandable and extendable (should we ever get another basic
> type :))
Also trivially easy with mixins. Normally I don't suggest doing
things this way but for basic types mixin is guaranteed to work.
```
enum basic_types = ["bool", "ubyte", "char", "byte", "ushort",
"wchar", "short", "uint", "dchar", "int", "ulong", "long"];
// to not import stdlib
alias AliasSeq(T...) = T;
string join(string[] s, string j) {
string a;
foreach(i; s)
a ~= i ~ j;
return a;
}
template basicTypeConvTargets(T) {
// again, I copy/pasted your code with very slight
modifications
string[] helper() {
string[] targets;
targets.length = basic_types.length;
size_t n = 0;
static foreach(t;basic_types)
{
if (is(T : mixin(t)))
{
targets[n++] = t;
}
}
return targets[0 .. n];
}
mixin("alias basicTypeConvTargets = AliasSeq!(" ~
helper().join(",") ~ ");");
}
pragma(msg, basicTypeConvTargets!ushort);
More information about the Digitalmars-d
mailing list