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

use*_*224 6 f# type-parameter type-providers f#-data

我正在使用FSharp.Data.JsonProviderTwitter推文.

使用此示例代码 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 )
Run Code Online (Sandbox Code Playgroud)

这会导致错误:

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.
Run Code Online (Sandbox Code Playgroud)

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

类型感染告诉我这个

val urls : FSharp.Data.JsonProvider<...>.DomainTypes.Url []
Run Code Online (Sandbox Code Playgroud)

但是在类型声明中不接受这一点.我假设"<...>"不是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
  ]
Run Code Online (Sandbox Code Playgroud)

Gus*_*rra 4

当您调用Twitter.Search.Tweetshttps://github.com/tpetricek/Documents/blob/master/Samples/Twitter.API/Twitter.fs#L284)时,其返回类型是 的域类型之一TwitterTypes.SearchTweets,这是一种类型JsonProvider<"references\\search_tweets.json">( https://github.com/tpetricek/Documents/blob/master/Samples/Twitter.API/Twitter.fs#L183 )的别名。

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