On 2016-12-02 10:11, Ethan Watson wrote:
On Friday, 2 December 2016 at 08:33:17 UTC, Jacob Carlborg wrote:
But wWhat about an anonymous class as ag0aep6g mentioned?

Usability I see is something that is key here. Having to do a typeof
yourself is one extra step away from an intuitive API.

So I tried making a definition like this:

mixin template BitPack( Descriptor instance, string NameAlias =
Descriptor.stringof )
{
  mixin( GenerateBitPackBody!( Descriptor, Descriptor.stringof )() );
}

...but I can't work out how to make the template evaluator treat
descriptor as a templated type without explicitly defining that type as
the first parameter.

Even trying:

mixin template BitPack( alias Descriptor ) if( is( typeof( Descriptor ) ) )

or:

mixin template BitPack( alias Descriptor ) if( __traits( compiles,
&Descriptor ) )

wasn't getting me valid code (in the case of the latter, it claims it's
not a valid template value argument; in the former, it meant I would
have needed to add additional constraints to the other BitPack template).

I bet there's a trick out there that'll let me do what I want. But I
won't go looking for it at least.

Using an alias without a template constraint works for me. No "typeof" is required when using the template:

template Foo(alias T)
{
    pragma(msg, __traits(identifier, T.tupleof[0]));
}

struct PackSize
{
    int size;
}

void main()
{
    mixin Foo!(
        new class {
            @PackSize( 3 ) int iSomeInt = 3;
            @PackSize( 1 ) bool bSomeBool = true;
            @PackSize( 4 ) int iSomeOtherInt;
        }
    );
}

The only difference now from your original example is "new class" instead of "struct".

--
/Jacob Carlborg

Reply via email to