gpt4 book ai didi

f# - 使用 F# TypeProvider 类型的类型注释,例如FSharp.Data.JsonProvider<...>.DomainTypes.Url

转载 作者:行者123 更新时间:2023-12-04 12:32:13 26 4
gpt4 key购买 nike

我正在使用 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.

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

类型推断向我展示了这一点
val urls : FSharp.Data.JsonProvider<...>.DomainTypes.Url []

但这在类型声明中不被接受。我假设“<...>”不是 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
]

最佳答案

当您调用 Twitter.Search.Tweets ( https://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

关于f# - 使用 F# TypeProvider 类型的类型注释,例如FSharp.Data.JsonProvider<...>.DomainTypes.Url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21402829/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com