F# type error: String vs Async<string>
F# type error: String vs Async<string>
我是编程新手,F# 是我的第一门 .NET 语言。
这是我目前编写的一些代码:
let downloadFromWebsite (url: string) =
async {
let uri = new System.Uri(url)
let webClient = new WebClient()
let! html = webClient.AsyncDownloadString(uri)
printfn "Read %d characters from %s" html.Length url
return html
}
let results = downloadFromWebsite @"http://plato.stanford.edu"
printf "%s" results
错误信息如下:
~vs8222.fsx(19,13): error FS0001: This expression was expected to have
type
string but here has type
Async
出了什么问题?我应该做哪些改变?
results
是 Async<string>
,而您使用的格式字符串需要 string
。您需要 运行 计算并获得结果,然后才能打印。您可以使用 Async.RunSynchronously
:
let results = downloadFromWebsite @"http://plato.stanford.edu" |> Async.RunSynchronously
printf "%s" results
我是编程新手,F# 是我的第一门 .NET 语言。
这是我目前编写的一些代码:
let downloadFromWebsite (url: string) =
async {
let uri = new System.Uri(url)
let webClient = new WebClient()
let! html = webClient.AsyncDownloadString(uri)
printfn "Read %d characters from %s" html.Length url
return html
}
let results = downloadFromWebsite @"http://plato.stanford.edu"
printf "%s" results
错误信息如下:
~vs8222.fsx(19,13): error FS0001: This expression was expected to have type string but here has type Async
出了什么问题?我应该做哪些改变?
results
是 Async<string>
,而您使用的格式字符串需要 string
。您需要 运行 计算并获得结果,然后才能打印。您可以使用 Async.RunSynchronously
:
let results = downloadFromWebsite @"http://plato.stanford.edu" |> Async.RunSynchronously
printf "%s" results