Pattern matching in D?

Chris M. via Digitalmars-d digitalmars-d at puremagic.com
Thu Oct 20 23:10:54 PDT 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


More information about the Digitalmars-d mailing list