Eliminating code duplication for static/nonstatic functions

Dmitry Olshansky dmitry.olsh at gmail.com
Fri Sep 20 08:32:20 PDT 2013


20-Sep-2013 19:20, Andrei Alexandrescu пишет:
> On 9/19/13 1:02 PM, Andrej Mitrovic wrote:
>> On 9/19/13, Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:
>>> I'm not sure I understand how that would work.
>>
>> -----
>> module test;
> [snip]
>
> Thanks, that's the simplest and most effective. I reduced it to:
>
> struct A
> {
>      enum hasState = false;
>      private mixin template funDef()
>      {
>          void fun(int x) { writeln(x); }
>      }
>      static if (hasState)
>          mixin funDef!();
>      else
>          static mixin funDef!();
> }
>
> void main()
> {
>      A a;
>      a.fun(42);
> }
>
> I see no way to extract the scaffolding into a library.

Shouldn't it as simple as a mixin-able template that simply forwards all 
methods of a type T (and if it has no state it does so statically).
With __traits(allMembers,...) to extract all publics, it must be doable.

The end result should be around the following...

struct A{
	//aliases type or contains state as required
	mixin extractState!T;
}



-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list