Option types and pattern matching.

Dmitry Olshansky via Digitalmars-d digitalmars-d at puremagic.com
Sun Oct 25 11:23:21 PDT 2015


On 25-Oct-2015 08:01, Nerve wrote:
> Hello D community! First time poster, I'm utterly fascinated with this
> language's mix of features. It's powerful and expressive.
>
> There are just two conveniences I'd like to see out of D. The first is
> pattern matching, a functional construct which can unwrap tuples or
> other containers, usually evaluates to a value (in most languages), and
> which almost always idiomatically enforces the programmer to match over
> every possible case of a given type.
>
> While ML-inspired languages like F# and OCaml have some good pattern
> matching syntax, it's not syntax which would fit in to D; I suggest
> taking inspiration of Rusts's matching construct.
>
> match x {
>      Some(7) => "Lucky number 7!",
>      Some(_) => "No lucky number.",
>      None => "No value found"
> }
>

I humbly believe that D may just add special re-write rule to the switch 
statement in order to allow user-defined switchable types. This goes 
along nicely with the trend - e.g. foreach statement works with anything 
having static range interfaces or opApply.

All in all we've seen a lot of examples of how it's done in the library 
but always somewhat cumbersome. The next big problem would be that 
switch is a statement not expression which limits use-cases of 
user-defined pattern matching.


-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list