Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does putting the first line of the expression on the same line as let not compile?

Tags:

syntax

f#

In F# the following statement will fail with the following errors

let listx2 = [1..10]
    |> List.map(fun x -> x * 2)
    |> List.iter (fun x -> printf "%d " x)

Block following this 'let' is unfinished. Expect an expression.

Unexpected infix operator in binding. Expected incomplete structured construct at or before this point or other token.

However the following will compile

let listx2 = 
    [1..10]
    |> List.map(fun x -> x * 2)
    |> List.iter (fun x -> printf "%d " x)

I also noticed that this compiles but has a warning

let listx2 = [1..10] |>
    List.map(fun x -> x * 2) |>
    List.iter (fun x -> printf "%d " x)

Possible incorrect indentation: this token is offside of context started at position (10:18). Try indenting this token further or using standard formatting conventions.

What is the difference between the first two statements?

like image 256
Daniel Little Avatar asked Sep 03 '16 23:09

Daniel Little


1 Answers

When you have

 let listx2 = [1..10]

you are implicitly setting the indent level of the next line to be at the same character as the [. As given by the following rule for offside characters from the spec:

Immediately after an = token is encountered in a Let or Member context.

So in the first example, the |> is indented less than [ so you get an error, but in the second they are the same, so it all works.

I am not quite sure why moving the |> to the end of the line only gives a warning though.

like image 102
John Palmer Avatar answered Oct 19 '22 11:10

John Palmer