使用F#TypeProvider类型的类型注释,例如FSharp.Data.JsonProvider< ...> .DomainTypes.Url [英] Type annotation for using a F# TypeProvider type e.g. FSharp.Data.JsonProvider<...>.DomainTypes.Url

查看:71
本文介绍了使用F#TypeProvider类型的类型注释,例如FSharp.Data.JsonProvider< ...> .DomainTypes.Url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the FSharp.Data.JsonProvider to read Twitter Tweets.

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

我想使用以下方式扩展推文中的网址

I want to expand the urls in the tweet with

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类型?

My problem is how to define the TypeProvider Type for url in the expandUrl function above?

类型推断告诉我这个

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

,但是在类型声明中不接受.我假设< ...>"不是F#synatx.

but this is not accepted in the type declaration. I assume "<...>" is not F# synatx.

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

How to do a type annotation for using a TypeProvider type e.g. 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
  ]

推荐答案

调用Twitter.Search.Tweets(的类型别名(

When you call Twitter.Search.Tweets (https://github.com/tpetricek/Documents/blob/master/Samples/Twitter.API/Twitter.fs#L284), the return type of that is one of the domain types of TwitterTypes.SearchTweets, which is a type alias for 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

Although in the tooltip it shows up as JsonProvider<...>.DomainTypes.Url, you'll have to use the type alias TwitterTypes.SearchTweets.DomainTypes.Url

这篇关于使用F#TypeProvider类型的类型注释,例如FSharp.Data.JsonProvider&lt; ...&gt; .DomainTypes.Url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆