softshipper
softshipper

Reputation: 34099

Expecting int return value

I have following code snippet, which evaluates an XML file:

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

The compiler shows the following error:

Error   1   This expression was expected to have type
    int    
but here has type
    unit    D:\f#\samples\Program.fs    16  5   samples

Why? What am I doing wrong?

Upvotes: 3

Views: 100

Answers (1)

N_A
N_A

Reputation: 19897

F# is whitespace delimited. That means that this:

for customer in input.GetCustomers() do
for order in customer.GetOrders() do
for line in order.GetOrderLines() do

needs to be more like this:

for customer in input.GetCustomers() do
  for order in customer.GetOrders() do
    for line in order.GetOrderLines() do

otherwise the scope of the body of the first for continues until the end of the current level of indentation (which includes the 0). You could use the old style of syntax to fix this by doing something like this:

for customer in input.GetCustomers() do
for order in customer.GetOrders() do
for line in order.GetOrderLines() do

...

done
done
done
0

but this is highly non-idiomatic at this point.

Upvotes: 2

Related Questions