/TP/TypeSafeSplit.fs

http://github.com/bleis-tift/TypeProviderSample · F# · 27 lines · 23 code · 4 blank · 0 comment · 1 complexity · 07257905108229927a77b061f237dd46 MD5 · raw file

  1. namespace StringUtil
  2. open Microsoft.FSharp.Core.CompilerServices
  3. open TPUtil
  4. open TPUtil.SimpleTypeProvider
  5. open TPUtil.StaticArgument
  6. type TSSplit = class end
  7. [<TypeProvider>]
  8. type SplitterProvider() =
  9. inherit SimpleTypeProviderBase<TSSplit> begin
  10. NameSpace = "StringUtil",
  11. StaticParams = [ StaticParameter.make<int> "count" ]
  12. end
  13. override this.GenSrc args =
  14. let count = (args |> List.find (fun a -> a.Name = "count")).Value :?> int
  15. let vars = List.init count (fun i -> "s" + (string i))
  16. let pat = vars |> String.concat "; "
  17. let tpl = vars |> String.concat ", "
  18. "let split =\n \
  19. let split' (sep: string) (str: string) =\n \
  20. match str.Split([| sep |], " + (string count) + ", StringSplitOptions.None) with\n \
  21. | [| " + pat + " |] -> (" + tpl + ")\n \
  22. | _ -> failwith \"not match.\"\n \
  23. split'"