digitalmars.D - Pattern matching in D?
- Chris M. (5/5) Oct 20 2016 So I know you can do some pattern matching with templates in D,
- Stefan Koch (2/7) Oct 20 2016 How is this diffrent from "switch-case" ?
- Dennis Ritchie (11/12) Oct 20 2016 A more laconic and convenient form of the recording conditions:
- Chris M. (12/19) Oct 20 2016 Rust's match is more powerful than a regular switch statement. A
- ArturG (19/26) Oct 21 2016 switch is a statement, rusts match is an expression and can
- Dennis Ritchie (11/16) Oct 20 2016 On this topic there were many discussions. Here are some of them:
- mogu (3/7) Oct 21 2016 Agreed. D has not macro, this makes argly syntax while using
- Mark (2/5) Oct 21 2016 AST macros are permanently off the table?
- default0 (10/16) Oct 21 2016 Unless you find a way to convince Walter and Andrei that its not
- Dennis Ritchie (5/12) Oct 21 2016 IMHO, the best option to do so to create an experimental
- Nick Sabalausky (4/9) Oct 21 2016 What I've been really wanting for a long time is the one-two combo of
- Chris M (4/17) Oct 23 2016 There is std.variant, though I haven't used it much myself and
- Nick Sabalausky (44/52) Oct 23 2016 This is one of those things where language support makes a big
- Nick Sabalausky (37/50) Oct 23 2016 Just to compare to equivalent D:
- Nick Treleaven (46/49) Oct 28 2016 I think the following should be better. Instead of Proxy we would
- Nick Treleaven (2/5) Nov 02 2016 https://issues.dlang.org/show_bug.cgi?id=16655
- Dennis Ritchie (5/5) Oct 28 2016 Someone may be, it will be interesting, in the C# 7 `switch` will
So I know you can do some pattern matching with templates in D, but has there been any discussion about implementing it as a language feature, maybe something similar to Rust's match keyword (https://doc.rust-lang.org/stable/book/patterns.html)? What would your guys' thoughts be?
Oct 20 2016
On Friday, 21 October 2016 at 02:16:44 UTC, Chris M. wrote:So I know you can do some pattern matching with templates in D, but has there been any discussion about implementing it as a language feature, maybe something similar to Rust's match keyword (https://doc.rust-lang.org/stable/book/patterns.html)? What would your guys' thoughts be?How is this diffrent from "switch-case" ?
Oct 20 2016
On Friday, 21 October 2016 at 02:40:45 UTC, Stefan Koch wrote:How is this diffrent from "switch-case" ?A more laconic and convenient form of the recording conditions: * No need to constantly write "case", "break", "case", "break", ... * You can use the "|", it facilitates the matching also inside the body "match" and allows the use of multiple patterns * Works with tuples and slices * More modern than the "switch" * etc. https://doc.rust-lang.org/stable/book/slice-patterns.html https://doc.rust-lang.org/stable/book/box-syntax-and-patterns.html
Oct 20 2016
On Friday, 21 October 2016 at 02:40:45 UTC, Stefan Koch wrote:On Friday, 21 October 2016 at 02:16:44 UTC, Chris M. wrote:Rust's match is more powerful than a regular switch statement. A case in a switch statement can only match one value in each case (or a range of values), whereas pattern matching can do everything Dennis described above. One important concept I'd also point out is that Rust's match statement can destructure a data type like structs or tuples, so it'll allow you to easily work with individual components of any compound data type. You can also look at how Haskell does it, it's a pretty great feature https://www.haskell.org/tutorial/patterns.html http://learnyouahaskell.com/syntax-in-functionsSo I know you can do some pattern matching with templates in D, but has there been any discussion about implementing it as a language feature, maybe something similar to Rust's match keyword (https://doc.rust-lang.org/stable/book/patterns.html)? What would your guys' thoughts be?How is this diffrent from "switch-case" ?
Oct 20 2016
On Friday, 21 October 2016 at 02:40:45 UTC, Stefan Koch wrote:On Friday, 21 October 2016 at 02:16:44 UTC, Chris M. wrote:switch is a statement, rusts match is an expression and can return a value. i posted this[1] templates a while ago with which you can probably do most of what rust can do with the match expressing (not tested havent looked into rust much and pattern matching isnt the main purpose of them), by combining existing D features. e.g. 5.call!(a => a == 3? "three" : a == 5? "five" : "nomatch").writeln; prints: five 5.call!((a){ a == 3? "three".writeln : a == 5? "five".writeln : null;}).writeln; prints: five 5 [1] http://melpon.org/wandbox/permlink/ngUYhp7SS6uY283bSo I know you can do some pattern matching with templates in D, but has there been any discussion about implementing it as a language feature, maybe something similar to Rust's match keyword (https://doc.rust-lang.org/stable/book/patterns.html)? What would your guys' thoughts be?How is this diffrent from "switch-case" ?
Oct 21 2016
On Friday, 21 October 2016 at 02:16:44 UTC, Chris M. wrote:So I know you can do some pattern matching with templates in D, but has there been any discussion about implementing it as a language feature, maybe something similar to Rust's match keyword (https://doc.rust-lang.org/stable/book/patterns.html)? What would your guys' thoughts be?On this topic there were many discussions. Here are some of them: http://forum.dlang.org/post/mhdcpnnydgspxllismlb forum.dlang.org http://forum.dlang.org/post/znfrdjkpxtixiydxpcbr forum.dlang.org http://forum.dlang.org/post/ugiypegvtdhhvzrmfuua forum.dlang.org The problem is that D is not macros, and the implementation of pattern matching without macros will not be very good. In turn, the implementation of macros in D - this is also not a good idea. Previously, there were ideas on the implementation of macros in D, but now they are no longer relevant: http://s3.amazonaws.com/dconf2007/WalterAndrei.pdf
Oct 20 2016
On Friday, 21 October 2016 at 06:50:26 UTC, Dennis Ritchie wrote:The problem is that D is not macros, and the implementation of pattern matching without macros will not be very good. In turn, the implementation of macros in D - this is also not a good idea.Agreed. D has not macro, this makes argly syntax while using mixin instead. Event C/C++ has c-macro to fix the syntax issues.
Oct 21 2016
On Friday, 21 October 2016 at 06:50:26 UTC, Dennis Ritchie wrote:Previously, there were ideas on the implementation of macros in D, but now they are no longer relevant: http://s3.amazonaws.com/dconf2007/WalterAndrei.pdfAST macros are permanently off the table?
Oct 21 2016
On Friday, 21 October 2016 at 11:49:42 UTC, Mark wrote:On Friday, 21 October 2016 at 06:50:26 UTC, Dennis Ritchie wrote:Unless you find a way to convince Walter and Andrei that its not gonna result in everyone defining their own sub-language within D, making D code harder to read for others and/or have good reasons for things they enable that currently cannot be done (read: have rather ugly and laborious/error-prone workarounds or simply no workarounds at all while being desirable things to want to do). At least as far as I remember those were the main points they were on about :o)Previously, there were ideas on the implementation of macros in D, but now they are no longer relevant: http://s3.amazonaws.com/dconf2007/WalterAndrei.pdfAST macros are permanently off the table?
Oct 21 2016
On Friday, 21 October 2016 at 12:17:30 UTC, default0 wrote:Unless you find a way to convince Walter and Andrei that its not gonna result in everyone defining their own sub-language within D, making D code harder to read for others and/or have good reasons for things they enable that currently cannot be done (read: have rather ugly and laborious/error-prone workarounds or simply no workarounds at all while being desirable things to want to do).IMHO, the best option to do so to create an experimental D-compiler, which will support macros. And, of course, working examples, which will show all the positive benefits of D with macros. Dreams... :D)
Oct 21 2016
On 10/20/2016 10:16 PM, Chris M. wrote:So I know you can do some pattern matching with templates in D, but has there been any discussion about implementing it as a language feature, maybe something similar to Rust's match keyword (https://doc.rust-lang.org/stable/book/patterns.html)? What would your guys' thoughts be?What I've been really wanting for a long time is the one-two combo of Nemerle's variants and pattern matching: https://github.com/rsdn/nemerle/wiki/Grok-Variants-and-matching
Oct 21 2016
On Friday, 21 October 2016 at 19:00:55 UTC, Nick Sabalausky wrote:On 10/20/2016 10:16 PM, Chris M. wrote:There is std.variant, though I haven't used it much myself and don't know how well it compares. Seems like that library would provide a good basis for providing pattern matching though.So I know you can do some pattern matching with templates in D, but has there been any discussion about implementing it as a language feature, maybe something similar to Rust's match keyword (https://doc.rust-lang.org/stable/book/patterns.html)? What would your guys' thoughts be?What I've been really wanting for a long time is the one-two combo of Nemerle's variants and pattern matching: https://github.com/rsdn/nemerle/wiki/Grok-Variants-and-matching
Oct 23 2016
On 10/23/2016 03:38 PM, Chris M wrote:On Friday, 21 October 2016 at 19:00:55 UTC, Nick Sabalausky wrote:This is one of those things where language support makes a big difference (like slices). Algebraic is the *closest* thing in D that compared to Nemerle's variants...But honestly, saying std.variant compares to Nemerle's variants is like saying C can do high-order functions, OOP, and has a module system. Yea, *technically* you can, but it's so clunky by comparison that you're really not getting much of the real benefit. One of the first examples on that page really highlights how it differs from D: -------------------------------- // An equivalent std.variant.Algebraic would be clunky by comparison: variant RgbColor { | Red | Yellow | Green | Different { red : float; green : float; blue : float; } } -------------------------------- string_of_color (color : RgbColor) : string { match (color) { | RgbColor.Red => "red" | RgbColor.Yellow => "yellow" | RgbColor.Green => "green" | RgbColor.Different (r, g, b) => $"rgb($r, $g, $b)" } } -------------------------------- D can get close, but it's just not so clean, wouldn't scale as well, and that really does make a difference (just like how many of D's features are argued by others to be "not that big a deal", but we know that it is because we use it and know that extra bit of polish D gives makes a big difference). And, yea, yea, Manu has a far better color color lib in D, but of course this is just an illustration of the language construct. It's one of those things (of which D really does have many - just not this one), that once you have it available and start using it, it's very liberating, and loosing it feels like having your hands tied.What I've been really wanting for a long time is the one-two combo of Nemerle's variants and pattern matching: https://github.com/rsdn/nemerle/wiki/Grok-Variants-and-matchingThere is std.variant, though I haven't used it much myself and don't know how well it compares. Seems like that library would provide a good basis for providing pattern matching though.
Oct 23 2016
On 10/23/2016 11:55 PM, Nick Sabalausky wrote:-------------------------------- // An equivalent std.variant.Algebraic would be clunky by comparison: variant RgbColor { | Red | Yellow | Green | Different { red : float; green : float; blue : float; } } --------------------------------Just to compare to equivalent D: -------------------------------- struct RgbColor_ // Don't clutter the namepsace { struct Red {} struct Yellow {} struct Green {} struct Different { float red; float green; float blue; } } alias RgbColor = Algenraic!( RgbColor_.Red, RgbColor_.Yellow, RgbColor_.Green, RgbColor_.Different, } -------------------------------- It's just...I mean, yea, it works, and you could probably DRY it up a little with a type contructing template ("alias RgbColor = DoMagic!RgbColor_"), but...meh... And then the pattern matching end would be similarly "ehh...meh...": -------------------------------- RgbColor color = ...; auto x = color.visit( (RgbColor_.Red a) => "red", (RgbColor_.Yellow a) => "yellow", (RgbColor_.Green a) => "green", (RgbColor_.Red a) => mixin(interpolateStr!`rgb(${a.red}, ${a.green}, ${a.blue})`), ); -------------------------------- Again, technically works, but...ehh, it's like doing slices or high-order funcs in C.
Oct 23 2016
On Monday, 24 October 2016 at 04:14:52 UTC, Nick Sabalausky wrote:It's just...I mean, yea, it works, and you could probably DRY it up a little with a type contructing template ("alias RgbColor = DoMagic!RgbColor_"), but...meh...I think the following should be better. Instead of Proxy we would have a bespoke mixin which might fix some of the workarounds below. In the unittest, using with(Color) should help, but I couldn't get that to compile (visit thinks invalid lambdas are being passed). import std.variant; struct Color { struct Custom { float red; float green; float blue; } //mixin NewTypes!`Red, Yellow, Green`; struct Red {} struct Yellow {} struct Green {} private auto impl = Algebraic!( Custom, Red, Yellow, Green)(); import std.typecons; mixin Proxy!impl; } unittest{ Color color; // assignment works but not ctor color = Color.Custom(1, 2, 3); assert(color.type == typeid(Color.Custom)); // FIXME: currently need impl auto x = color.impl.visit!( (Color.Red) => "red", (Color.Yellow) => "yellow", (Color.Green) => "green", (Color.Custom c) => ctFormat!`rgb(%s, %s, %s)`(c.red, c.green, c.blue) ); assert(x == "rgb(1, 2, 3)"); } // TODO: implement ct parsing auto ctFormat(string s, Args...)(Args args){ import std.format; return format(s, args); }
Oct 28 2016
On Friday, 28 October 2016 at 11:53:16 UTC, Nick Treleaven wrote:In the unittest, using with(Color) should help, but I couldn't get that to compile (visit thinks invalid lambdas are being passed).https://issues.dlang.org/show_bug.cgi?id=16655
Nov 02 2016
be extended syntax for pattern matching: https://github.com/dotnet/roslyn/blob/features/patterns/docs/features/patterns.md Original post: https://github.com/dotnet/roslyn/issues/206
Oct 28 2016