期望 int return 值
Expecting int return value
我有以下代码片段,它评估了一个 XML 文件:
open System
open System.IO
open System.Xml.Linq
open FSharp.Data
open System.Net
type InputXml = XmlProvider<"C:\Temp\sample.xml">
[<EntryPoint>]
let main argv =
let input = InputXml.Load("C:\Temp\sample.xml")
for customer in input.GetCustomers() do
for order in customer.GetOrders() do
for line in order.GetOrderLines() do
printfn "Customer: %s, Order: %s, Item: %s, Quantity: %d"
customer.Name order.Number line.Item line.Quantity
Console.ReadLine() |> ignore
0 // return an integer exit code
编译器显示以下错误:
Error 1 This expression was expected to have type
int
but here has type
unit D:\f#\samples\Program.fs 16 5 samples
为什么?我做错了什么?
F# 是用空格分隔的。这意味着:
for customer in input.GetCustomers() do
for order in customer.GetOrders() do
for line in order.GetOrderLines() do
需要更像这样:
for customer in input.GetCustomers() do
for order in customer.GetOrders() do
for line in order.GetOrderLines() do
否则第一个 for
的正文范围将一直持续到当前缩进级别结束(包括 0
)。您可以使用旧式语法通过执行以下操作来解决此问题:
for customer in input.GetCustomers() do
for order in customer.GetOrders() do
for line in order.GetOrderLines() do
...
done
done
done
0
但这在这一点上是非常不合惯用语的。
我有以下代码片段,它评估了一个 XML 文件:
open System
open System.IO
open System.Xml.Linq
open FSharp.Data
open System.Net
type InputXml = XmlProvider<"C:\Temp\sample.xml">
[<EntryPoint>]
let main argv =
let input = InputXml.Load("C:\Temp\sample.xml")
for customer in input.GetCustomers() do
for order in customer.GetOrders() do
for line in order.GetOrderLines() do
printfn "Customer: %s, Order: %s, Item: %s, Quantity: %d"
customer.Name order.Number line.Item line.Quantity
Console.ReadLine() |> ignore
0 // return an integer exit code
编译器显示以下错误:
Error 1 This expression was expected to have type
int
but here has type
unit D:\f#\samples\Program.fs 16 5 samples
为什么?我做错了什么?
F# 是用空格分隔的。这意味着:
for customer in input.GetCustomers() do
for order in customer.GetOrders() do
for line in order.GetOrderLines() do
需要更像这样:
for customer in input.GetCustomers() do
for order in customer.GetOrders() do
for line in order.GetOrderLines() do
否则第一个 for
的正文范围将一直持续到当前缩进级别结束(包括 0
)。您可以使用旧式语法通过执行以下操作来解决此问题:
for customer in input.GetCustomers() do
for order in customer.GetOrders() do
for line in order.GetOrderLines() do
...
done
done
done
0
但这在这一点上是非常不合惯用语的。