I have the following function. It is used to turn parts of a string into a Discriminated union case. I had specific ones and have been deduping and generalising the code.
let extractor ty (characters:list<char>)=
//find the UnionCaseInfo for the named subtype of Token (ty)
let uci = (Microsoft.FSharp.Reflection.FSharpType.GetUnionCases(typeof<Token>) |> Array.filter (fun u->u.Name.Equals(ty))).[0]
//construct the chosen type from the passed in ElementValue (l)
let maker l = Microsoft.FSharp.Reflection.FSharpValue.MakeUnion(uci,[|l|]) :?> Token
match characters with
| '('::t -> match t with
| '!'::t2 -> match t2 with
| '\''::t3 -> let (entries,rest) = t3 |> entriesUntil '\''
(maker(LiteralInsertion(stringOf entries)),trimHeadIf ')' rest)
| '\''::t2 -> let (entries,rest) = t2 |> entriesUntil '\''
(maker(LiteralValue(stringOf entries)),trimHeadIf ')' rest)
| '$'::t2 -> let (entries,rest) = t2 |> entriesUntil ')'
(maker(CaptureValue(stringOf entries)), rest)
| _ -> failwith "Expecting !'Insertion', $Capture or 'Literal' following ("
| l -> (maker(DataCellValue),l)
I would like to do something like the following:
let extractor<'T> (characters:list<char>) =
etc...
| l -> ('T(DataCellValue),l)
but that didn't work (so I ended up with the reflection approach)
thx in advance