type union1 =
| Case1 of string
| Case2 of int
let union1s = seq { for i in 1..5 do yield case2 i }
Come cambio union1s
ad una sequenza di tipo seq<int>
?
Qualcosa di simile:
let matchCase item =
match item with
| Case1 x -> x
| Case2 x -> x
let case2s = Seq.map matchCase union1s
Questo tentativo non funziona perché matchcase non può restituire due tipi diversi.
Le risposte suggerite hanno lo stesso problema (se ho capito bene)
let matchCaseOpt = function
| Case1 x -> Some x
| Case2 x -> Some x
| _ -> None
let case2s = Seq.choose matchCaseOpts unions1s
L'espressione Alcuni x aspetta aspetta che tipo stringa di opzione nella partita per Case2
ho risolto il mio particolare caso d'uso usando una DU di sequenze.
type Union1s =
| Case1s of seq<string>
| Case2s of seq<int>
Si prega di notare che non è una risposta suggerita. Case1 è sbagliato, dovrebbe essere None. – Gustavo