alias and mixin
evilrat via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Sep 1 03:57:41 PDT 2014
On Sunday, 31 August 2014 at 12:01:43 UTC, evilrat wrote:
> On Sunday, 31 August 2014 at 11:43:03 UTC, ketmar via
> Digitalmars-d-learn wrote:
>> On Sun, 31 Aug 2014 11:26:47 +0000
>> evilrat via Digitalmars-d-learn
>> <digitalmars-d-learn at puremagic.com>
>> wrote:
>>
>>> alias myint = mixin("int"); // <- basic type expected blah
>>> blah
>> mixin("alias myint = "~"int"~";");
>> ?
>
> wow, it works. i don't even think inverting it O_o
> you saved my day, thanks.
ugh... somewhat related to original problem, made simple template
for quick check if type present or provide alternative.
now that is something i am not understand.
sorry for my stupidness :(
--- code
struct A {}
alias coolStruct = typeByName!("MyStruct", A);
// oops, looks like alias is set to template itself, though
// code completion(mono-d) shows it is correctly aliased to A
//
// error: template instance is used as a type
void doSomething(coolStruct cs)
{
.. do something with struct ..
}
--- template itself
template typeByName(alias Name, Replace)
{
static if ( __traits(compiles, (mixin(Name~`.sizeof`))) )
mixin(`alias typeByName = ` ~ Name ~ `;`);
else
static if ( (is(Replace == class) || is(Replace == struct)) )
alias typeByName = Replace;
}
More information about the Digitalmars-d-learn
mailing list