Re: generating switch case from compile time sequence of functions

2019-07-14 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 14 July 2019 at 19:26:41 UTC, Sjoerd Nijboer wrote:

The code it must generate for `doSwitch!(foo, bar)()` is
`{
switch (int)
{
foo:
foo();
return;
bar:
bar();
return;
}
}`



I'd probably just do

void doSwitch(items...)(int i) {
   switch(i) {
  static foreach(idx, item; items) {
 case idx:
 item();
 return;
  }
   }
}


That should work pretty simply.


enum temp = [FunctionNames].join(", ");

enum switchEnum = "{" ~ temp ~ "};";


Were you trying to do a mixin here? The error you mention below 
is trying to use this switchEnum thing as a type... and it isn't 
a type, it is just a string. the mixin() is necessary to compile 
it into code and thus create that type.


mixin("enum switchEnum = { " ~ temp ~ "}");

but I think even attempting this is overcomplicating.


static foreach (name; FunctionNames)
{
name ~ " : " ~ name ~ "(); break;";
}


ditto down here too.


generating switch case from compile time sequence of functions

2019-07-14 Thread Sjoerd Nijboer via Digitalmars-d-learn
I am trying to create a template function with a switch case 
inside it.

The function signature is:
`static void doSwitch(T...)(int i)`

The code it must generate for `doSwitch!(foo, bar)()` is
`{
switch (int)
{
foo:
foo();
return;
bar:
bar();
return;
}
}`

It would be nice if this function would cast `i` to an enum too 
so that I can put down a breakpoint in a debugger and maybe add 
some logging, but that is not strictly neccesary.



The code I have right now is:
`
template switchEnum(FunctionNames...)
{
enum temp = [FunctionNames].join(", ");

enum switchEnum = "{" ~ temp ~ "};";
}

static void doSwitch(FunctionNames...)(int i)
{
auto functionName = cast(switchEnum!FunctionNames) i;

switch (functionName)
{
static foreach (name; FunctionNames)
{
name ~ " : " ~ name ~ "(); break;";
}
}
}
`
But I can't get it to work and am hitting a dead end.
The error I get:
`Error: switchEnum!(foo, bar) is used as a type`



Re: Substitutions with writef() or format()?

2019-07-14 Thread Ron Tarrant via Digitalmars-d-learn

On Sunday, 14 July 2019 at 13:45:38 UTC, Ron Tarrant wrote:

'Morning, all.

If these lines:

   string currency = format("$%,.02f", 11_234_456.99);
   writeln(currency);

Will result in:

   $11,234,456.99

Why don't these lines:

   string notCurrency = format("%,", 11_234_456);
   writeln(notCurrency);

result in:

   11,234,456

???

Instead of a range violation?


Never mind. This works:

   string notCurrency = format("%,?d", ',', 11_234_456);
   writeln(notCurrency);



Substitutions with writef() or format()?

2019-07-14 Thread Ron Tarrant via Digitalmars-d-learn

'Morning, all.

If these lines:

   string currency = format("$%,.02f", 11_234_456.99);
   writeln(currency);

Will result in:

   $11,234,456.99

Why don't these lines:

   string notCurrency = format("%,", 11_234_456);
   writeln(notCurrency);

result in:

   11,234,456

???

Instead of a range violation?




Re: Windows segfault, need brief help

2019-07-14 Thread Boris Carvajal via Digitalmars-d-learn

On Saturday, 13 July 2019 at 16:39:51 UTC, Anonymouse wrote:


Thank you!

Filed as https://issues.dlang.org/show_bug.cgi?id=20048.


https://github.com/dlang/druntime/pull/2675


Re: Heterogeneous Variadic Arguments with Other Arguments

2019-07-14 Thread harakim via Digitalmars-d-learn

On Sunday, 14 July 2019 at 06:05:13 UTC, evilrat wrote:

On Sunday, 14 July 2019 at 03:51:14 UTC, harakim wrote:

I wanted to do this:
package int serialize(byte[] destination, int offset, T...)(T 
values)




Can't really tell what's going on without call site and what do 
you exactly mean by "heterogeneous arguments with other 
arguments", but this thing is syntax error. You declared a 
template here, and template parameters (compile-time 
parameters) must come first.


   int serialize(T...)(byte[] destination, int offset, T values)

This is the case if you wanted to pass that buffer at runtime, 
not sure if you can do this with CT parameters, though it 
shouldn't prevent you from forcing CTFE evaluation by assigning 
result to another CT parameter or enum.



But if by "other arguments" you mean passing different types 
there is untyped variadics with a bit different behavior(see 
[1])



[1] 
https://dlang.org/spec/function.html#d_style_variadic_functions


Thanks. That answered my question. I was hoping to do this:
int serialize(T...)(byte[] destination, int offset, T values)

And now I understand a lot more about variadic functions and type 
arguments.


Re: Heterogeneous Variadic Arguments with Other Arguments

2019-07-14 Thread evilrat via Digitalmars-d-learn

On Sunday, 14 July 2019 at 03:51:14 UTC, harakim wrote:

I wanted to do this:
package int serialize(byte[] destination, int offset, T...)(T 
values)




Can't really tell what's going on without call site and what do 
you exactly mean by "heterogeneous arguments with other 
arguments", but this thing is syntax error. You declared a 
template here, and template parameters (compile-time parameters) 
must come first.


   int serialize(T...)(byte[] destination, int offset, T values)

This is the case if you wanted to pass that buffer at runtime, 
not sure if you can do this with CT parameters, though it 
shouldn't prevent you from forcing CTFE evaluation by assigning 
result to another CT parameter or enum.



But if by "other arguments" you mean passing different types 
there is untyped variadics with a bit different behavior(see [1])



[1] 
https://dlang.org/spec/function.html#d_style_variadic_functions