I have the following:
type union1 =
| Case1 of string
| Case2 of int
let union1s = seq { for i in 1..5 do yield case2 i }
How do I change union1s
to a sequence of type seq<int>
?
Something like:
let matchCase item =
match item with
| Case1 x -> x
| Case2 x -> x
let case2s = Seq.map matchCase union1s
This attempt does not work because matchCase can not return two different types.
The suggested answers have the same problem (if I understand correctly)
let matchCaseOpt = function
| Case1 x -> Some x
| Case2 x -> Some x
| _ -> None
let case2s = Seq.choose matchCaseOpts unions1s
The expression Some x expects expects type Option string in the match for Case2
I have solved my particular use-case by using a DU of sequences.
type Union1s =
| Case1s of seq<string>
| Case2s of seq<int>