alex
alex

Reputation: 40

Verbose syntax doesn't work in lightweight mode F#

Microsoft F# manual says that verbose syntax is always enabled in F#. If I understand that correctly, it means that F# code, written using verbose syntax, has to produce the same result whether lightweight syntax is enabled or not.

However, the code snippet below prints the number 6 ten times

#light "off"

let f x = for i = 1 to 10 do printfn "%d" x done in f 6

while the following generates the error: Unexpected keyword 'in' in binding. Expected incomplete structured construct at or before this point or other token.

let f x = for i = 1 to 10 do printfn "%d" x done in f 6

What is the structural difference between these two snippets and why is the error generated when lightweight syntax is enabled?

Upvotes: 1

Views: 151

Answers (1)

AMieres
AMieres

Reputation: 5004

This verbose code:

#light "off"

let f x = for i = 1 to 10 do printfn "%d" x done in f 6

is equivalent to this lightweight code:

let f x = 
    for i = 1 to 10 do 
        printfn "%d" x 
f 6

in the lightweight mode the indentation defines the blocks, in verbose mode do ... done is the inner block of the for and let ... in ... is the block of the expression f 6.

Regarding why it complains when lightweight is on there are 2 possibilities:

  • It could be a bug in the parser. Very few people use verbose and it may have gone unnoticed.

  • It's also possible that both versions are no fully compatible. The documentation does say:

even if you enable lightweight syntax, you can still use verbose syntax for some constructs.

Notice how it says "some" and not "all".

Upvotes: 0

Related Questions