하스켈 또는 스칼라만큼 강력한 전용 패턴 일치 기능이 없습니다.
이미 알아 냈으므로 (템플릿 처리 된) 함수 또는 델리게이트를 오버로드 및 호출하는 것은 제한된 패턴 일치 형식입니다. 인수 유형 만 일치시킬 수 있습니다.
에서 패턴 일치를 사용하여 컴파일 시간 템플릿 인수를 사용할 수 있습니다. 거기에 객체의 내용을 추출 할 수도 없습니다. 그러나 유형의 내용을 추출 할 수 있습니다.
예 : 당신이 관심이 있다면
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
}
, 당신은 http://d-programming-language.org/template.html 좀보고 할 수 있습니다.
는 '이다'- 표현식 유형에 대한 패턴 일치에 또 다른 방법입니다 ("IsExpression"에 대한 검색)
http://d-programming-language.org/expression.html를 참조하십시오.
흠 ... 패턴 일치로 정확히 무엇을 의미합니까? 논리 프로그래밍에 대해 이야기하고 있습니까? – Mehrdad
@Mehrdad Haskell의 패턴 일치는 C++의 템플릿 전문화와 비슷하다고 생각합니다. – Arlen
@Red : 허 ... 나는 그것이 패턴 매칭이라고 불리는 이유를 잘 모르겠다. 그것이 Scheme과 같다고 말하는 것이 맞습니까? (람다와 매크로는 꽤 많은 패턴 매칭 자이기 때문에)? 그렇다면, 나는 우리가 어디로 갈지를 생각합니다. – Mehrdad