Re: static switch/pattern matching

2016-06-25 Thread John via Digitalmars-d-learn
On Saturday, 25 June 2016 at 12:35:39 UTC, Lodovico Giaretta wrote: On Saturday, 25 June 2016 at 12:30:22 UTC, Lodovico Giaretta wrote: If you want this to work, you need your lambdas to take the casted value as a parameter: Thanks.

Re: static switch/pattern matching

2016-06-25 Thread Lodovico Giaretta via Digitalmars-d-learn
On Saturday, 25 June 2016 at 12:30:22 UTC, Lodovico Giaretta wrote: If you want this to work, you need your lambdas to take the casted value as a parameter: void test(T)(T value) { int i; string s; match!(value, int, (val) => i = val, string, (val) => s = val

Re: static switch/pattern matching

2016-06-25 Thread Lodovico Giaretta via Digitalmars-d-learn
On Saturday, 25 June 2016 at 10:39:09 UTC, John wrote: Thanks for the help, both. This appeared to work, until I realised the lambda isn't static: void match(T, cases...)() { static if (cases.length == 1) cases[0](); else static if (cases.length > 2) { static if

Re: static switch/pattern matching

2016-06-25 Thread John via Digitalmars-d-learn
On Saturday, 25 June 2016 at 09:12:12 UTC, Lodovico Giaretta wrote: On Saturday, 25 June 2016 at 09:07:19 UTC, Lodovico Giaretta wrote: Instead of passing functions to match!, pass pairs of arguments, like this: match!(T, int, writeln("Matched int"), is(T : SomeObject),

Re: static switch/pattern matching

2016-06-25 Thread Lodovico Giaretta via Digitalmars-d-learn
On Saturday, 25 June 2016 at 09:07:19 UTC, Lodovico Giaretta wrote: Instead of passing functions to match!, pass pairs of arguments, like this: match!(T, int, writeln("Matched int"), is(T : SomeObject), writeln("Derives from SomeObject"); ); Now, in the

Re: static switch/pattern matching

2016-06-25 Thread ketmar via Digitalmars-d-learn
also, there is a subtle bug in matcher. sorry. ;-)

Re: static switch/pattern matching

2016-06-25 Thread ketmar via Digitalmars-d-learn
On Saturday, 25 June 2016 at 08:46:05 UTC, John wrote: Anyone able to improve on it? q hack: template tyma(T, Cases...) { import std.traits; template GetFunc(size_t idx) { static if (idx >= Cases.length) { static assert(0, "no delegate for match"); } else static if

Re: static switch/pattern matching

2016-06-25 Thread Lodovico Giaretta via Digitalmars-d-learn
On Saturday, 25 June 2016 at 08:46:05 UTC, John wrote: Writing a long series of "static if ... else" statements can be tedious and I'm prone to leaving out the crucial "static" after "else", so I was wondered if it was possible to write a template that would resemble the switch statement, but

static switch/pattern matching

2016-06-25 Thread John via Digitalmars-d-learn
Writing a long series of "static if ... else" statements can be tedious and I'm prone to leaving out the crucial "static" after "else", so I was wondered if it was possible to write a template that would resemble the switch statement, but for types. Closest I came up to was this: void