2010-01-04 8 views
5

C'è un modo per nidificare chiamate a modelli attivi?C'è un modo per nidificare le chiamate ai pattern attivi di F #?

Qualcosa di simile a questo:

type Fnord = 
| Foo of int 

let (|IsThree|IsNotThree|) x = 
    match x with 
    | x when x = 3 -> IsThree 
    | _ -> IsNotThree 

let q n = 
    match n with 
    | Foo x -> 
    match x with 
    | IsThree -> true 
    | IsNotThree -> false 
    // Is there a more ideomatic way to write the previous 
    // 5 lines? Something like: 
// match n with 
// | IsThree(Foo x) -> true 
// | IsNotThree(Foo x) -> false 

let r = q (Foo 3) // want this to be false 
let s = q (Foo 4) // want this to be true 

O è l'incontro seguito da un altro incontro il modo migliore per andare?

+3

+1 per il Fnord. – bmargulies

+0

Il maledetto linguaggio è illeggibile. Seriamente - cosa può fare che Python non può? –

+8

@lpthnc: Pattern matching? – Chuck

risposta

12

Funziona. Hai solo i modelli all'indietro.

type Fnord = 
| Foo of int 

let (|IsThree|IsNotThree|) x = 
    match x with 
    | x when x = 3 -> IsThree 
    | _ -> IsNotThree 

let q n = 
    match n with 
    | Foo (IsThree x) -> true 
    | Foo (IsNotThree x) -> false 

let r = q (Foo 3) // want this to be true 
let s = q (Foo 4) // want this to be false 
+0

Sono un po 'sorpreso questo funziona, in realtà. –

+0

@AlexeyRomanov Bene, la possibilità di annidare i modelli nel loro principale vantaggio rispetto ad altre forme di spedizione, ad es. metodi virtuali. –

Problemi correlati