键入注释以使用F#TypeProvider类型,例如FSharp.Data.JsonProvider< ...> .DomainTypes.Url

时间:2014-01-28 10:19:52

标签: f# type-providers type-parameter f#-data

我正在使用FSharp.Data.JsonProvider阅读Twitter推文。

使用此示例代码 https://github.com/tpetricek/Documents/tree/master/Samples/Twitter.API

我想用

扩展推文中的网址
let expandUrl (txt:string) (url:Search.DomainTypes<...>.DomainTypes.Url) = 
    txt.Replace( url.Url, url.ExpandedUrl )

这导致错误:

Lookup on object of indeterminate type based on information prior to this program point. 
A type annotation may be needed prior to this program point to constrain the type of the object.

我的问题是如何在上面的expandUrl函数中为url定义TypeProvider类型?

类型感染告诉我这个

val urls : FSharp.Data.JsonProvider<...>.DomainTypes.Url []

但是在类型声明中不接受。我假设“&lt; ...&gt;”不是F#synatx。

如何使用TypeProvider类型进行类型注释,例如FSharp.Data.JsonProvider<...>.DomainTypes.Url ?

以下是完整的代码段:

open TwitterAPI // github.com/tpetricek/Documents/tree/master/Samples/Twitter.API 
let twitter = TwitterAPI.TwitterContext( _consumerKey, _consumerSecret, _accessToken, _accessTokenSecret )
let query = "water"
let ts = Twitter.Search.Tweets(twitter, Utils.urlEncode query, count=100)

let ret = 
  [ for x in ts.Statuses do
      // val urls : FSharp.Data.JsonProvider<...>.DomainTypes.Url []
      let urls = x.Entities.Urls 
      // fully declarated to help the type inference at expandUrl
      let replace (txt:string) (oldValue:string) (newValue:string) = 
          txt.Replace( oldValue, newValue)
      // Error:
      // Lookup on object of indeterminate type based on information prior to this program point. 
      // A type annotation may be needed prior to this program point to constrain the type of the object. 
      // This may allow the lookup to be resolved.
      let expandUrl (txt:string) (url:FSharp.Data.JsonProvider<_>.DomainTypes.Url) = 
          replace txt url.Url url.ExpandedUrl
      let textWithExpandedUrls = Array.fold expandUrl x.Text urls
      yield textWithExpandedUrls
  ]

2 个答案:

答案 0 :(得分:4)

当您致电Twitter.Search.Tweetshttps://github.com/tpetricek/Documents/blob/master/Samples/Twitter.API/Twitter.fs#L284)时,其返回类型是TwitterTypes.SearchTweets的某种域类型,它是JsonProvider<"references\\search_tweets.json">的类型别名({ {3}})。

虽然在工具提示中显示为JsonProvider<...>.DomainTypes.Url,但您必须使用类型别名TwitterTypes.SearchTweets.DomainTypes.Url

答案 1 :(得分:0)

我有一个类似的问题,试图找出如何使用FSharp.Data HtmlProvider

我正在使用维基百科来获取有关美国presidents的信息。 HtmlProvider在发现该网页中的各种表格方面做得非常出色,但我想将用于处理一行“总统数据”的逻辑提取到一个名为processRow的单独函数中。

问题是试图找出processRow参数row的这种行的类型。以下代码可以解决这个问题:

#load "Scripts\load-references.fsx"

open FSharp.Data

let presidents = new HtmlProvider<"https://en.wikipedia.org/wiki/List_of_Presidents_of_the_United_States">()

let ps = presidents.Tables.``List of presidents``

ps.Headers |> Option.map (fun hs -> for h in hs do printf "%s " h)

printfn ""

type Presidents = ``HtmlProvider,Sample="https://en.wikipedia.org/wiki/List_of_Presidents_of_the_United_States"``.ListOfPresidents

let processRow (row:Presidents.Row) =
  printfn "%d %s" row.``№`` row.President2

ps.Rows |> Seq.iter processRow

我没有输入Presidents的长类型别名,我使用Visual Studio自动完成,猜测List of presidents的类型可以从以Html开头的内容中找到,它是完整的,有四个单背引号。