2013-01-08 20 views

回答

2
public macro m(to, k) 
{ 
    def toInteger(literal) 
    { 
    | <[ $(i : int) ]> => i 
    | _ => Message.Error(literal.Location, $"'$literal' integer literal expected"); 0; 
    } 

    def to = toInteger(to); 

    def createCases(i) 
    { 
    | i when (i > 0) => <[case: | $i => $(i + 1) ]> :: createCases(i - 1); 
    | _ => [ <[case: | _ => 0 ]> ] 
    } 

    <[ 
    match ($k) 
    { 
     ..$(createCases(to)) 
    } 
    ]> 
} 

http://nemerle.org/Macros_tutorial#Matching_match-cases

+0

完美,謝謝。 –

相關問題