Jump to page: 1 2
Thread overview
Pattern matching in D?
Oct 21, 2016
Chris M.
Oct 21, 2016
Stefan Koch
Oct 21, 2016
Dennis Ritchie
Oct 21, 2016
Chris M.
Oct 21, 2016
ArturG
Oct 21, 2016
Dennis Ritchie
Oct 21, 2016
mogu
Oct 21, 2016
Mark
Oct 21, 2016
default0
Oct 21, 2016
Dennis Ritchie
Oct 21, 2016
Nick Sabalausky
Oct 23, 2016
Chris M
Oct 24, 2016
Nick Sabalausky
Oct 24, 2016
Nick Sabalausky
Oct 28, 2016
Nick Treleaven
Nov 02, 2016
Nick Treleaven
Oct 28, 2016
Dennis Ritchie
October 21, 2016
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?


October 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?

How is this diffrent from "switch-case" ?
October 21, 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
October 21, 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:
>> 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" ?

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-functions
October 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
October 21, 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.
October 21, 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:
>> 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" ?

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/ngUYhp7SS6uY283b
October 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.pdf

AST macros are permanently off the table?
October 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:
>> 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
>
> AST macros are permanently off the table?

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)
October 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)
« First   ‹ Prev
1 2