I recently stumbled over the D programming Language and i really like it. You can programm really high-level while having full Hardware access like in C.
coming from a rather functional background (Haskell,scala) I´m searching for a way to pattern match in D, but i found nothing on http://www.digitalmars.com/d/.
In Haskell pattern matching is supported by the language itself.
In Scala it´s achieved by case classes or extractors(normal objects with an unapply method).
Is it possible to do this in D?
the receive method in std.concurrency which is used to do concurrency in an actor-style like in erlang and scala takes a bunch of functions and pattern matheses on these.
But i think it´s not as flexible as in other languages. Can you uses guards ? Can you extract the Object´s contents like it´s possible in scala ?
No pattern matching as known from Haskell is built in into language, but D has very generic compile time and reflection capabilities that will allow you to match type and their structure in library. For matching runtime values you can use only ordinary
if/switch… constructs; lazy evaluation of function arguments might also help with implementation of some matching-at-runtime techniques.Template Constraints will allow you to create function (template) overloads based on any expression evaluated at compile time (D allows you to execute almost all normal code during compilation). You can also use
static iffor similar effect. This will practically allow you to match type structure. This is also commonly used technique in D.You might find code of std.algorithm interesting, look for isInputRange and similar functions – they perform matching on type structure – they restrict type of argument to be of certain typeclass
Some directions for compile time reflection: