Re: Mixin replacement for switch...case?

2012-10-24 Thread Jerome
Remark 1: I understand that your mixin will be expanded into 
cascaded if...else statements. It would probably be more 
efficient to expand into switch...case, don't you think?


Oh! I've just figured out that it is not a mixin, but a function 
template.


Re: Mixin replacement for switch...case?

2012-10-24 Thread Jerome

Thanks Philippe! Great solution!

I have two remarks.

Remark 1: I understand that your mixin will be expanded into 
cascaded if...else statements. It would probably be more 
efficient to expand into switch...case, don't you think?


Remark 2: I infer from your code that the "delegate" keyword is 
not mandatory, so my solution could also be called like this:


mixin Select!(value,
  if0, { then0(); },
  if1, { then1(); },
  if2, { foo(); bar(); },
  { thenDefault(); }
);

instead of:

mixin Select!(value,
  if0, delegate { then0(); },
  if1, delegate { then1(); },
  if2, delegate { foo(); bar(); },
  delegate { thenDefault(); }
);

Is that correct?




Re: Mixin replacement for switch...case?

2012-10-23 Thread Jerome

On Tuesday, 23 October 2012 at 10:40:12 UTC, Daniel Kozák wrote:
I think this should be possible, look for eg. to std.bitmanip 
bitfields template


On Tuesday, 23 October 2012 at 09:47:55 UTC, Jerome wrote:

No answer. Should I assume that it is not possible?
That's something that could be done in C with a simple macro. 
I really would like to know to what extent mixins are a 
replacement for C macros for generating boilerplate code.


Thanks Daniel! It's exactly what I was looking for.  ;-)


Re: Mixin replacement for switch...case?

2012-10-23 Thread Jerome
OK. I have done my homework and answered my own question based on 
the Duff's Device example in the Language Reference page for 
Mixins.


The solution (not variadic though) would be:

mixin template Select!(alias value,
  alias if0, alias then0,
  alias if1, alias then1,
  alias if2, alias then2,
  alias thenDefault)
{
  switch(value) {
case if0 : { then0(); } break;
case if1 : { then1(); } break;
case if2 : { then2(); } break;
default : thenDefault();
  }
}

and it is used this way:

mixin Select!(value,
  if0, delegate { then0(); },
  if1, delegate { then1(); },
  if2, delegate { foo(); bar(); },
  delegate { thenDefault(); }
);

no gain at all verbosity-wise I'm afraid... nevermind.


Re: Mixin replacement for switch...case?

2012-10-23 Thread Jerome

No answer. Should I assume that it is not possible?
That's something that could be done in C with a simple macro. I 
really would like to know to what extent mixins are a replacement 
for C macros for generating boilerplate code.


Mixin replacement for switch...case?

2012-10-22 Thread Jerome

Hi!

This is a question from a complete newbie.

Is there a way to replace switch...case statements by a mixin 
template, maybe a variadic mixin template (does such a thing 
exist?).


What I would want to achieve is to have this kind of syntax:

mixin Select!(value,
  if0, { then0(); },
  if1, { then1(); },
  if2, { foo(); bar(); },
  { thenDefault(); }
);

to replace this:

switch(value) {
  case if0 : { then0(); } break;
  case if1 : { then1(); } break;
  case if2 : { foo(); bar(); } break;
  default : thenDefault();
}

The reason I ask this is because I almost never use fall through 
and the verbosity of the switch statement has been driving me 
crazy.