22

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 ?

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
KIMA
  • 1,077
  • 6
  • 14
  • Hm... what exactly do you mean by pattern matching? Are you talking about logic programming? – user541686 Jun 11 '11 at 17:06
  • @Mehrdad I think pattern matching in Haskell is sort of like template specialization in C++. – Arlen Jun 11 '11 at 20:59
  • @Red: Huh... I'm not sure I understand why it's called pattern matching though. Is it correct to say it's like Scheme (since lambdas and macros are pretty much pattern matchers)? If it is, then I think I see where we're going. – user541686 Jun 11 '11 at 21:04
  • @Mehrdad It's called pattern matching because it's pattern matching, *not* template specialization. If you want to know exactly what it is, google it. – Jim Balter Jun 13 '11 at 01:36

4 Answers4

11

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 if for 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:

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Michal Minich
  • 2,387
  • 1
  • 22
  • 30
10

There is no dedicated pattern-matching feature as powerful as in Haskell or Scala.

As you have already figured out, overloading and calling (templated) functions or delegates is a restricted form of pattern matching. You can match on the argument types only.

You can pattern match on compile time template arguments. It is not possible to extract an object's contents there either. But you can extract a type's contents.

Eg :

import std.stdio, std.conv;
template match(T...){
    enum match = "default case";
}
template match(string a : "x", int b : 1, int  c){
    enum match = "matched 1, b="~to!string(b);
}
template match(int a, string b, int c : 100){
    enum match = "matched 2, b="~b;
}
template match(T : T[]*[]){
    enum match = "matched 3, an array of pointers to an array of "~T.stringof;
}


void main(){
    int a=100;
    writeln(match!("x",1,5));        // "matched 1, b=1"                                                                                                     
    writeln(match!(12,"str"));       // "default case"                                                                                                       
    writeln(match!(12,"str",100));   // "matched 2, b=str"                                                                                                   
    writeln(match!(int*[]*[]));      // "matched 3, an array of pointers to an array of int*"                                                                
    //writeln(match!(12,"str",a));   // would be error, because 'a'                                                                                            
                                     // is not evaluable during compile time                                                                                 
}

If you are interested, you may want to have a look at http://d-programming-language.org/template.html .

'is' - Expressions are another way to pattern match on types, see

http://d-programming-language.org/expression.html (search for "IsExpression").

tgehr
  • 197
  • 5
1

The sumtype package provides sum types and pattern matching on them.

antoyo
  • 11,097
  • 7
  • 51
  • 82
-1

If you don't insist on using D2 and/or Phobos, you can use the tango library. It has a regex module which you can use. [Edit] There is also a regex module in D2/Phobos [/Edit] If you insist on using D2 and/or Phobos, you can try porting it. It shouldn't be too hard.

Tango is a alternative std lib. Many tango developers and users don't like the way D2 is going and most of the stick to D1. That's why there are only some incomplete port to D2 of it available.

There is also the project scregex which provides statically compiled regular expressions. I used it myself already and it worked. But I think it also is D1-only. Yet, it works with both, phobos and tango.

Marenz
  • 2,722
  • 3
  • 20
  • 19