Re: Pattern matching via switch?

2020-03-17 Thread Steven Schveighoffer via Digitalmars-d-learn
On 3/17/20 8:04 AM, rikki cattermole wrote: On 18/03/2020 12:59 AM, Steven Schveighoffer wrote: I think he's looking for object pattern matching. i.e. you give it an Object, and it runs a block of code based on the derived type. In case this syntax is unknown: if (MyObject1 myObject = cast

Re: Pattern matching via switch?

2020-03-17 Thread rikki cattermole via Digitalmars-d-learn
On 18/03/2020 12:59 AM, Steven Schveighoffer wrote: I think he's looking for object pattern matching. i.e. you give it an Object, and it runs a block of code based on the derived type. In case this syntax is unknown: if (MyObject1 myObject = cast(MyObject1)obj) { ... } else

Re: Pattern matching via switch?

2020-03-17 Thread Steven Schveighoffer via Digitalmars-d-learn
implemented the feature), or use cascaded if statements: I've just given a fast look at the thread, so maybe I'm wrong, but this [1] should be ok for pattern matching using plain and simple Phobos ... [1] https://dlang.org/phobos/std_variant.html#.visit I think he's looking for object pattern

Re: Pattern matching via switch?

2020-03-17 Thread Paolo Invernizzi via Digitalmars-d-learn
if statements: I've just given a fast look at the thread, so maybe I'm wrong, but this [1] should be ok for pattern matching using plain and simple Phobos ... [1] https://dlang.org/phobos/std_variant.html#.visit

Re: Pattern matching via switch?

2020-03-16 Thread DanielG via Digitalmars-d-learn
I've been playing around with this via inheritance lately (I'm aware of the sumtype package but specifically wanted to use objects instead of structs), had some good results. It involves a bit of boilerplate though. I'm essentially using the visitor pattern + an anonymous class implementing a

Re: Pattern matching via switch?

2020-03-15 Thread Steven Schveighoffer via Digitalmars-d-learn
://docs.microsoft.com/en-us/dotnet/csharp/pattern-matching It is an example from c#. object is the top type in that language. https://en.wikipedia.org/wiki/Top_type D doesn't support this natively. The closest you can get is something akin to what aliak wrote (you would need to write something, not sure

Re: Pattern matching via switch?

2020-03-15 Thread 12345swordy via Digitalmars-d-learn
/pattern-matching It is an example from c#. object is the top type in that language. https://en.wikipedia.org/wiki/Top_type

Re: Pattern matching via switch?

2020-03-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 3/14/20 3:04 PM, 12345swordy wrote: I.E. switch (object)     case Type1 t1:     case Type2 t2:     case Type3 t3: Is this a class object and you are trying to determine at runtime which derived type it is and perform an action based on that? Or are you trying to switch on the type of

Re: Pattern matching via switch?

2020-03-14 Thread 12345swordy via Digitalmars-d-learn
On Saturday, 14 March 2020 at 20:52:30 UTC, aliak wrote: On Saturday, 14 March 2020 at 19:04:28 UTC, 12345swordy wrote: [...] You can use the sumtype package (https://code.dlang.org/packages/sumtype): [...] That simply to much verbiage.

Re: Pattern matching via switch?

2020-03-14 Thread aliak via Digitalmars-d-learn
On Saturday, 14 March 2020 at 19:04:28 UTC, 12345swordy wrote: I.E. switch (object) case Type1 t1: case Type2 t2: case Type3 t3: You can use the sumtype package (https://code.dlang.org/packages/sumtype): alias T = SumType!(Type1, Type2, Type3); T(object).match!( (Type1 t1)

Re: Pattern matching via switch?

2020-03-14 Thread Luhrel via Digitalmars-d-learn
On Saturday, 14 March 2020 at 19:04:28 UTC, 12345swordy wrote: I.E. switch (object) case Type1 t1: case Type2 t2: case Type3 t3: As far as I know, there's no way to do that in a switch. However, you can do something like this: --- void main() { auto i = new Type1();

Pattern matching via switch?

2020-03-14 Thread 12345swordy via Digitalmars-d-learn
I.E. switch (object) case Type1 t1: case Type2 t2: case Type3 t3:

Re: Writing pattern matching macros in D.

2017-03-06 Thread Jacob Carlborg via Digitalmars-d-learn
On 2017-03-06 17:27, Deech wrote: I was thinking something on the order of Scala's pattern matching using apply/unapply methods. http://www.artima.com/pins1ed/extractors.html. That should be possible. Although not as a macro and not with the same nice syntax. Something like this should

Re: Writing pattern matching macros in D.

2017-03-06 Thread Deech via Digitalmars-d-learn
On Monday, 6 March 2017 at 08:27:13 UTC, David Nadlinger wrote: On Monday, 6 March 2017 at 02:20:02 UTC, Deech wrote: […] add pattern matching to the language as a macro. D doesn't have macros per se. However, template metaprogramming and mixins can replace them in many cases. Which

Re: Writing pattern matching macros in D.

2017-03-06 Thread David Nadlinger via Digitalmars-d-learn
On Monday, 6 March 2017 at 02:20:02 UTC, Deech wrote: […] add pattern matching to the language as a macro. D doesn't have macros per se. However, template metaprogramming and mixins can replace them in many cases. Which particular form of pattern matching do you have in mind? You won't get

Re: Writing pattern matching macros in D.

2017-03-05 Thread sarn via Digitalmars-d-learn
On Monday, 6 March 2017 at 02:20:02 UTC, Deech wrote: Hi all, I've been reading up on D's metaprogramming features and was wondering if it was possible to use them to add pattern matching to the language as a macro. The template mixin feature seems to require putting the new syntax in strings

Writing pattern matching macros in D.

2017-03-05 Thread Deech via Digitalmars-d-learn
Hi all, I've been reading up on D's metaprogramming features and was wondering if it was possible to use them to add pattern matching to the language as a macro. The template mixin feature seems to require putting the new syntax in strings. I was hoping there's an alternative. Thanks! -deech

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

Re: Emulation macros and pattern matching on D

2015-06-05 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/5/15 10:15 AM, Dennis Ritchie wrote: On Friday, 5 June 2015 at 13:13:15 UTC, Steven Schveighoffer wrote: string foo(string mode, string value) { return `writefln(mode ` ~ mode ~ `: %s, ` ~ value ~ `);`; } void main() { mixin(foo(Y, 3)); mixin(foo(X, 2)); } Thanks. It looks

Message passing pattern matching

2014-01-29 Thread Casper Færgemand
have two empty structs named AddTid and RemoveTid, and I pass those along. I assume some bytes must be passed in their place to tell of their type, but that's fine. It does feel rather hacked, however. I'm aware that D doesn't support excessively fancy pattern matching, like what is found

Re: Message passing pattern matching

2014-01-29 Thread Casper Færgemand
A small example: while (true) { receive( (Tid tid, AddTid _) {some code} (Tid tid, RemoveTid _) {some other code} (string s) {broadcast stuff} ) } struct AddTid {} struct RemoveTid {}

Re: Message passing pattern matching

2014-01-29 Thread Stanislav Blinov
On Wednesday, 29 January 2014 at 21:50:28 UTC, Casper Færgemand wrote: A small example: while (true) { receive( (Tid tid, AddTid _) {some code} (Tid tid, RemoveTid _) {some other code} (string s) {broadcast stuff} ) } struct AddTid {} struct RemoveTid {} From where I sit

pattern matching

2011-09-06 Thread %u
template factorial(int n) { const factorial = n * factorial!(n-1); } template factorial(int n : 1) { const factorial = 1; } i think this pattern matching or like it, can i do the same thing with regular function int factorial(int n) { return n* factorial(n-1); return 1 ; } int factorial(int n

Re: pattern matching

2011-09-06 Thread bearophile
%u: is that work? In D unfortunately there is no pattern matching on the run-time values of function arguments. But I don't really understand what you are asking me, sorry. Bye, bearophile

Re: pattern matching

2011-09-06 Thread Jonathan M Davis
On Tuesday, September 06, 2011 21:05:54 %u wrote: template factorial(int n) { const factorial = n * factorial!(n-1); } template factorial(int n : 1) { const factorial = 1; } i think this pattern matching or like it, can i do the same thing with regular function int factorial(int n