/TP/TypeSafeSplit.fs
http://github.com/bleis-tift/TypeProviderSample · F# · 27 lines · 23 code · 4 blank · 0 comment · 1 complexity · 07257905108229927a77b061f237dd46 MD5 · raw file
- namespace StringUtil
-
- open Microsoft.FSharp.Core.CompilerServices
- open TPUtil
- open TPUtil.SimpleTypeProvider
- open TPUtil.StaticArgument
-
- type TSSplit = class end
-
- [<TypeProvider>]
- type SplitterProvider() =
- inherit SimpleTypeProviderBase<TSSplit> begin
- NameSpace = "StringUtil",
- StaticParams = [ StaticParameter.make<int> "count" ]
- end
-
- override this.GenSrc args =
- let count = (args |> List.find (fun a -> a.Name = "count")).Value :?> int
- let vars = List.init count (fun i -> "s" + (string i))
- let pat = vars |> String.concat "; "
- let tpl = vars |> String.concat ", "
- "let split =\n \
- let split' (sep: string) (str: string) =\n \
- match str.Split([| sep |], " + (string count) + ", StringSplitOptions.None) with\n \
- | [| " + pat + " |] -> (" + tpl + ")\n \
- | _ -> failwith \"not match.\"\n \
- split'"