Re: RFC to: my need for 'static switch' and CT 'static variables'

2021-11-25 Thread Alexey via Digitalmars-d-learn
On Friday, 26 November 2021 at 00:41:34 UTC, Elronnd wrote: you are right. thanks

Re: RFC to: my need for 'static switch' and CT 'static variables'

2021-11-25 Thread Elronnd via Digitalmars-d-learn
static if (...) { } else static if (...) { } else { static assert(0); }

Re: RFC to: my need for 'static switch' and CT 'static variables'

2021-11-25 Thread Alexey via Digitalmars-d-learn
On Thursday, 25 November 2021 at 22:00:15 UTC, Alexey wrote: I would want an static 'switch here', I mean something like ```D static switch (v.mode) { default: static assert(false, "v.mode" is invalid) case "gs_w_d": // etc... } ```

RFC to: my need for 'static switch' and CT 'static variables'

2021-11-25 Thread Alexey via Digitalmars-d-learn
} } } ``` currently it works kind of ok. but what it's missing is check on what none of [static if]s have worked. Ideally, I would want an static 'switch here', so I could make `static assert(false)` in it. and/or I'd like some sort of [compile time variable] to change it if [static if]s have worked and check such [compile time variable] later.

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 (is(typeof(cases

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), w

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 implementation

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&d 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 (isCalla

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 f

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 match

Re: static switch

2012-06-12 Thread cal
On Tuesday, 12 June 2012 at 08:35:44 UTC, Dmitry Olshansky wrote: It doesn't. It's easy to check anyway. Not for me :) I just saw that it worked, and wondered if it worked statically. Thanks!

Re: static switch

2012-06-12 Thread bearophile
cal: Does a switch statement acting on a template parameter act just like a chain of static if-else's? That is, does it just generate the code for the matching case? See: http://d.puremagic.com/issues/show_bug.cgi?id=6921 Bye, bearophile

Re: static switch

2012-06-12 Thread Dmitry Olshansky
On 12.06.2012 10:13, cal wrote: Does a switch statement acting on a template parameter act just like a chain of static if-else's? That is, does it just generate the code for the matching case? enum E { A, B, C } class Blah(E param) { void foo() { switch(param) { case(E.A) : blah; case(E.B) : ..

static switch

2012-06-11 Thread cal
Does a switch statement acting on a template parameter act just like a chain of static if-else's? That is, does it just generate the code for the matching case? enum E { A, B, C } class Blah(E param) { void foo() { switch(param) { case(E.A) : blah; case(E.B) : def