Type Provider ã«ããã³ã³ãã¤ã«æããã°ã©ãã³ã°
ãã¯ã¤ã«ããéãéå°æ¸ã®æ¸è©ä»¥å¤ã«ãã¡ããã¨ãã¯ã¤ããããã¨ãã¦ããã¨ããå ±åã§ã(å
F# 3.0 ã§ä½¿ããããã«ãªãäºå®ã® Type Provider ã§ãããããããã£ãã使ã£ã¦ã¿ã¾ããã
ä»åã®ããã°ã©ã 㯠Visual Studio 11 Developer Preview ã使ç¨ãã¦ãã¾ãã®ã§ãå®éã«åããã¦ã¿ãã人ã¯ã¤ã³ã¹ãã¼ã«ãã¦ãã ããã
Type Provider ã£ã¦ï¼
ã¾ããType Provider ã£ã¦ä½ãªã®ããã¨ãããã¨ã«ã¤ãã¦ã§ãã
Type Provider ã¨ããã®ã¯ã
- ã³ã³ãã¤ã«æã«(ããªããã»ã¹ã¨ãã§ã¯ãªãã»ã»ã»ã¨æã)
- ã³ã¼ãçæã«é ¼ããã«
- åãçæããä»çµã¿
ã®ãã¨ã ã¨ç解ãã¦ãã¾ãã
F# 3.0 Information Rich Programming(PDF) ãè¦ãéãã® TypeProvider ã®ä¸çªã®ç®çã¯ããã³ã¼ãçæã«ãããªãåã®çæãã«ããããã«æãã¾ãã
ã©ããããã¨ãã¨ããã¨ãä»ã¾ã§ã¯
- ãªã½ã¼ã¹ã«ã¢ã¯ã»ã¹ããããã«ãã¶ã¤ããããã£ã¦ãVS ãè£ã§ã³ã¼ããçæãã
- DB ã¨ããã¨ãããããã«ãã¶ã¤ããããã£ã¦ãVS ãè£ã§ã³ã¼ããçæãã (DataSet / Linq to SQL / Entity Framework / ...)
- Web ãµã¼ãã¹ã使ãããã«å®åçãªã³ã¼ããæ¸ãã¾ãããããã㯠T4 ãªã©ã§ãè¶ãæ¿ã
- WPF ãªã©ã§ MVVM ãã¿ã¼ã³ãé©ç¨ããããã« ViewModel ãªã©ã§å®åçãªã³ã¼ããæ¸ãã¾ãããããã㯠T4 ãªã©ã§ãè¶ãæ¿ã
ãªã©ãªã©ãã³ã¼ãçæ (ãã¨ã¯ãªãã¬ã¯ã·ã§ã³ï¼) ã«é ¼ã£ã¦ããå ´é¢ã¨ããã®ã¯çµæ§ããã¾ãã
Type Provider ã¯ãã®åé¡ã解決ãããã¨ã«éç¹ãç½®ãã¦ããããã§ãã
ä¾ãã°ãDB ã¨ã®ããåããè¡ããããã°ã
type SQL = SqlConnection<"Server=...">
ãã®ããã« 1 è¡æ¸ãã ã㧠DB ã®ã¹ãã¼ãããåãçæãã¦ãããããã«ãªãã¾ãã
ä»ã¾ã§ååãæ¸ãã¦ããã¨ããã«ãæ¥ç¶æååãæ¸ãã¦ãã¾ãã
ã©ããããããã«æ¸ããæ
å ±ããã¨ã«ã㦠Type Provider ãã³ã³ãã¤ã«æã«åãçæãã¦ãããããã§ãã
åã®ä»£ããã«å¤ãæ¸ãããã¨ã«ãã£ã¦åºããä¸ç
ããããããã®ã¨ã³ããªã®æ¬é¡ã§ãã
Type Provider ãå°å
¥ããã«ããããã¸ã§ããªãã¯ã®ãã©ã¡ã¼ã¿ã¨ãã¦å¤ãå
¥ãããã¨ãã§ããããã«ãªã£ããããªã®ã§ãã
ã»ã»ã»ãããC++ ã®ãã³ãã¬ã¼ãã¨åãããã«ã
ã¨ãããã¨ã¯ã§ãããF# ã§ãã§ããããã«ãªãã£ã¦ãã¨ãããªãã§ããããã³ã³ãã¤ã«æããã°ã©ãã³ã°ãã
ãã£ã¦ã¿ã
ã§ããããªæ°ã¯ããã»ã»ã»ã§ããã£ã¦ã¿ãªãã¨åãããªãï¼
ã¨ãããã¨ã§ããã£ã¦ã¿ã¾ããã
ç¬èªã® Type Provider ãå®ç¾©ããã®ã«ã¯ããã¤ãæ¹æ³ãããããã§ãããä»å㯠F# ã®æååãã³ã³ãã¤ã«ãã¦åãä½ãæ¹æ³ã§ããã¾ããã
ããã 㨠T4 ã¨ãã¾ãå¤ãããªããããªæ°ãããªãããªãã§ãããçæãããã³ã¼ãã¯è¡¨ã«å
¨ãåºã¦ããªãã®ã§ã¡ãã£ã¨ã ãããæãã§ãã
Type ãçæããæ¹æ³ããã£ã¨æ°è»½ã«ä½¿ããããã«ãªãã°ããã£ã¨æ軽ã«ããã£ã¨ç¶ºéºã« Type Provider ã使ããããã«ãªãããããã¾ããã
ã³ã¼ãèªä½ã¯ github ã«ä¸ãã¦ããã®ã§ãèå³ã®ããæ¹ã¯ã©ããã
bleis-tift/TypeProviderSample · GitHub
TP ã¨ããã½ãªã¥ã¼ã·ã§ã³ãæ¬ä½ã§ãTPClient ã¯ãµã³ãã«ã«ãªã£ã¦ãã¾ãã
ãã®ã³ã¼ããæ¸ãã«å½ããã以ä¸ã®æ å ±ãåèã«ãã¾ããã
- API Only - Stack Exchange
- First example of a very simple type provider
- Mindscape Blog » Blog Archive » F# type providers – as if by magic…
å®è£ æ¹éã¨ãã¦ã¯ä¸çªä¸ã®ãã®ã¨åãã§ãã
ä½ã£ã TypeProvider
ä½ã£ãã®ã¯ã
- åå®å ¨ãª split
- åå®å ¨ãª regex
ã® 2 ã¤ã§ãã
ããä¼¼ããããªé¨åã¯ããªãããã®ã§ããããã¾ã¨ããã©ã¤ãã©ãªçãªãã®ãä½ãã¾ããã
SimpleTypeProvider
ç¬èªã® TypeProvider ã§ä½ãã§ããã®ï¼ã¨ãããã¨ãç¥ãããæ¹ã¯ãããã¯é£ã°ãã¦åå®å ¨ãª split ã¸ã©ããã
SimpleTypeProvider ã¯ç¬èªã® TypeProvider ãä½ãããã§é¢åãªä½æ¥ãè©ä»£ãããã¦ããã¾ãã
SimpleTypeProvider.fs ã«ã¯
- StaticParameter
- StaticArgument
- SimpleTypeProvider
ã® 3 ã¤ã® module ããæ§æããã¦ãã¦ãSimpleTypeProvider module ã¯ããã«
Info ã¬ã³ã¼ã- SimpleTypeProviderBase ã¯ã©ã¹
ããæ§æããã¾ãã
éè¦ãªã®ã¯ãã® 2 ã¤ã®åSimpleTypeProviderBase ã§ãã
ç¬èªã® TypeProvider 㯠SimpleTypeProvider ãç¶æ¿ãããã®ã³ã³ã¹ãã©ã¯ã¿ã¨ã㦠Info ã¬ã³ã¼ãã®å¤ã渡ãã¾ãã
(* å®è£ ãå¤æ´ãã¾ããã type A = class end [<TypeProvider>] type AProvider() = inherit SimpleTypeProviderBase begin { NameSpace = "é©å½ãªåå空é" ProvideType = typeof<A> StaticParams = [ StaticParameter.make "static parameterã®åå" typeof<static parameterã®å>; ... ] OpenModules = [ "System" ] GenSrc = ã³ã¼ãçæé¢æ° } end *) type A = class end [<TypeProvider>] type AProvider() = inherit SimpleTypeProviderBase<A> begin NameSpace = "é©å½ãªåå空é", StaticParams = [ StaticParameter.make<static parameterã®å> "static parameterã®åå"; ... ], (* èªåå®è£ ããããã£ã使ãç¨ã«ããã®ã§ãããã©ã«ãå¤ã§ããå ´åã¯çç¥å¯è½ OpenModules = [ "System" ] *) end override this.GenSrc args = ã³ã¼ãçæé¢æ°ã®å®è£
ããã ãã§ãç¬èªã® Type Provider ãä½ããã¨ãã§ãã¾ãã
ããã§ãstatic parameter ã¨ããã®ãã¸ã§ããªãã¯ã®ã«ãã³ã®ä¸ã«æ¸ããã©ã¡ã¼ã¿ã«ãªã£ã¦ãããStaticParameter.make ã®ç¬¬ä¸å¼æ°ã«æå®ããæåå㯠VS ã®ã¤ã³ããªã»ã³ã¹ã«è¡¨ç¤ºãããããã¾ãã
ã³ã¼ãçæé¢æ°ã¯ãStaticArgument.t list ãåãåã£ã¦ãæååã¨ãã¦è¨è¿°ããé¢æ°ãè¿ãé¢æ°ãæå®ãã¾ãã
StaticParameter.t ã§ã¯ãªã StaticArgument.t ã«ãªã£ã¦ããã®ã¯ããã®é¢æ°ãå®è¡ãããã¨ãã«ã¯ãã§ã«å¼æ°ã®å¤ã渡ã£ã¦ãã¦ããããããä¿æãã¦ããããã§ãã
åå®å ¨ãª split
æååã split ããã®ã¯ãããããã¨ã§ãã
ããã¦ãåå²å¾ã®è¦ç´ æ°ãã³ã³ãã¤ã«åã«æ±ºã¾ã£ã¦ããã¨ããç¶æ³ãå°ãªãããããã§ãããã
ãããã.NET Framework ã® Split ã¡ã½ããã¯æ±ç¨çã§ããå¿
è¦ããããããé
åã§è¿ã£ã¦ãã¾ãã
ããã«ãã¿ã¼ã³ãããããããã¨ããã¿ã¼ã³ããããå®å
¨ã§ã¯ãªãã¨ããè¦åãç¡è¦ãããã
let a, b = match postCode.Split([| "-" |], 2, StringSplitOptions.None) with | [| a; b |] -> (a, b) | _ -> failwith "oops!"
ã®ããã«æ¬æ¥ä¸è¦ãªã±ã¼ã¹ãè¨è¿°ããå¿ è¦ãããã¾ãã
ããã (ã³ã¼ãçæã«é ¼ããã«) 解決ããããã« Type Provider ã使ãã¾ããã
åå®å
¨ãª split (TSSplit) ã使ãã¨ãã³ã¼ãã¯ãã®ããã«ãªãã¾ãã
[<Generate>] type PostCodeSplitter = TSSplit<2> // ... let a, b = postCode |> PostCodeSplitter.split "-"
åå®å ¨ãª regex
æ£è¦è¡¨ç¾ã split åæ§ããã使ãã¨æãã¾ã(ãã¼ãµã³ã³ããã¼ã¿ã使ããã¨ãã話ã¯ç½®ãã¦ããã¨ãã¦)ã
ããããæ£è¦è¡¨ç¾ã¯ (Boost.Xpressive ãªã©ã®ä¾ã¯ç½®ãã¦ããã¦) å®è¡æã«è§£æããã¾ãã
ãã®ããããã£ããéçåä»ãã®è¨èªã使ã£ã¦ããã«ãããããããæ£è¦è¡¨ç¾ã®ææ³ãééãã¦ããå ´åã¯å®è¡æã¨ã©ã¼ã¨ãªã£ã¦ãã¾ãã¾ãã
ããã (å¤æ
çãªè¨è¿°ãããã«) 解決ããããã« Type Provider ã使ãã¾ããã
åå®å
¨ãª regex (TSRegex) ã®åºæ¬çãªä½¿ãæ¹ã¯ä»¥ä¸ã®éãã§ãã
[<Generate>] type CellPhoneNumberPat = TSRegex< @"(\d{3})-(\d{4})-(\d{4})" > // ... let b, c = match str |> CellPhoneNumberPat.match' with | Some(_, b, c) -> b, c | None -> "", ""
ãã®ä¾ã§ãä¾ãã°æ£è¦è¡¨ç¾ãééããã¨ãã¾ãã
ããã¨ãtype CellPhoneNumberPat... ã®è¡ã§ã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªãã¾ãã
ããããã¨ã©ã¼ã¡ãã»ã¼ã¸ã¯ä¾å¤ãªãã¸ã§ã¯ãã® Message ã表示ãããã®ã§ãé常ã«ããããããã§ãã
ä¼¼ããããªä¾ã以ä¸ã®ã¨ã³ããªã«ããã®ã§ãåèã«ã©ããã
F# 3.0 Type Provider 正規表現のCompile-time構文チェック - 無料でプログラミングを教えます日記
ããã«ãTSRegex ã§ã¯ã°ã«ã¼ããéçã«è§£éããmatch' ã®æ»ãå¤ã®åãå¤ããããã«ãªã£ã¦ãã¾ãã
ãã㯠TSSplit ã¨åããããªæãã§ãããããå®ç¨çãªä¾ã«ãªã£ã¦ããã¨æãã¾ãã
ææ°çã§ã¯ãååä»ãã®ã°ã«ã¼ãã«ã対å¿ãã¾ããã
[<Generate>] type UrlPat = TSRegex< @"(?<Scheme>[^:]+)://(?<Host>[^/]+)(?<Path>/.*)" > let extractPath url = let result = UrlPat.nameMatch url result.Path.Value
ååãªãã®ã°ã«ã¼ã㯠_1 ã®ãããªååã§ã¢ã¯ã»ã¹ã§ãã¾ãã
ç½ çãªä½ã
è²ã ã¨å¼ã£ããã£ãé¨åãããã®ã§ããã®ãããã®èª¬æã§ãã
ã¢ã»ã³ããªã®çæ
TP/CompiledType.fs ã® compile é¢æ°ã§ã³ã¼ããã³ã³ãã¤ã«ãã¦ããã®ä¸ã® Type ãåãåºãã¦ãããã§ãããæåã¯ã¢ã»ã³ããªãã¡ã¢ãªå
ã«çæãããã¨ãã¦ãã¾ããã
ãããã¾ãåããªãã®ã§ãããã¬ã§è¿½ã£ã¦è¦ãã®ã§ãããå
¨ç¶ä¾å¤åºãªãããã¦ã©ããã使ãã¨åã®çæã«ãåãåºãã«ãé¢æ°å¼ã³åºãã«ãæåãã¡ãããããããããã¾ããã§ããã
ã¨ã©ã¼ã¡ãã»ã¼ã¸ã¯
ãã¤ã㪠ãã¡ã¤ã« '<ä¸æ>' ãéãã¨ãã«ã¨ã©ã¼ãçºçãã¾ãã
ã§ãã
ãã¡ã¤ã«çæãã¦ãªãã®ã«éããã¨ãã¦ããã®ã§ãã¤ã³ã¡ã¢ãªãããããã¦ä¸æãã¡ã¤ã«ã«ããã¨ãã¾ãããã¾ããã
æ±ãã¤ã³ã¡ã¢ãªã§ããæ¹æ³ï¼
é¢æ°ã®å®ç¾©æ¹æ³
ç¾å¨ãå®éã«å¼ã³åºãããé¢æ°ã®å®ç¾©ã¯ãããªæãã«ãªã£ã¦ãã¾ãã
let split = let split' (sep: string) (str: string) = ... split'
æåã¯ãã£ã¨ã·ã³ãã«ã«ã
let split (sep: string) (str: string) = ...
ã¨ãã¦ã¿ãã®ã§ãããããã ã¨ã³ã¼ãä¸ã¯ã«ãªã¼åããã¦ããã«ãé¢ããããå¼ã³åºãã¨ãã«ã¯ã¿ãã«å½¢å¼ã«ãªã£ã¦ãã¾ãã¾ããã
ãªã®ã§ãFSharpFunc ã使ã£ã¦ãããããã«åããã©ãæ¸ãæ¹ã«ãªã£ã¦ãã¾ãã
dll ãã¤ãã¿ã£ã±ãªãã«ãªã£ã¦ãã¾ã
Type Provider ã® dll ã¯æ±ããå²ã¨é¢åã§ãgithub ã«ä¸ãããããªæ§æã«ãã㨠dll ãã©ãã¤ããã¤ãã¿ã£ã±ãªãã«ãªã£ã¦ãã¾ã£ã¦ãããã³ã³ãã¤ã«ãã§ããªãç¶æ³ã«é¥ãã¾ãã
ãã®ãããéçºä¸ã¯ã½ãªã¥ã¼ã·ã§ã³ãåãã
- éå§åä½ããå¤é¨ããã°ã©ã ã®éå§ãã«ã㦠VS 11 ã® exe ãæå®ãã (C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe ã¨ããããªæã)
- ã³ãã³ãã©ã¤ã³å¼æ°ã« Type Provider ã® dll ã使ãã½ãªã¥ã¼ã·ã§ã³ã絶対ãã¹ã§æå®ãã
ã¨ããæ¹æ³ã使ã£ã¦ãã¾ããã
ãããã¸ãããä½ããã£ã¨ããæ¹æ³ããããªãæãã¦ã»ããã§ãã
ã¨ãããã¨ã§
Type Provider ã使ããã¨ã§ãF# ã§ãã³ã³ãã¤ã«æã«è²ã
ã¨ã§ããããã«ãªãã¾ããã
ç¾ç¶ã§ã¯æ
å ±ãå°ãªããã㤠API ã®æ´åãããã¦ããªããããªç¶æ
ãªã®ã§ãæ³¥èãæãã«ãªã£ã¦ãã¾ã£ã¦ãã¾ãã
ããã«ãæåã«æ¸ããããã« Type Provider ã®ä¸»ç¼ã¯ã³ã³ãã¤ã«æããã°ã©ãã³ã°ã«ã¯ç½®ããã¦ããªãããã§ãã
ã§ããããã辺ã¯æéããã¤ã«ã¤ãã¦è§£æ±ºããã¦ããã¨æ¥½è¦³ãã¦ãã¾ãã
å°ãªãã¨ããèªåã®ä¸ã§ã¯ T4 ã¯å®å
¨ã«ãããªãåã«ãªãã¾ãã(ãã ã F# ã«éã)ã
Type Provider ã«ãã£ã¦ F# ã«æ°ããªä¸çãéãããã¨ã¯ã¾ãééããªãã®ã§ããã²çããã Type Provider ã«ããã³ã³ãã¤ã«æããã°ã©ãã³ã°ã楽ããã§ãã ããï¼