TheSpiceMustFlow
TheSpiceMustFlow

Reputation: 21

Unexpected keyword error in simple F# code

I'm creating a tiny function that will generate an alphanumeric string randomly like so

open System

let randomStr (len : int) : string =
    let rand = new Random() 
    let ch = Array.concat [| [|'a' .. 'z'|];[|'A' .. 'Z'|];[|'0' .. '9'|] |]
    let sz = Array.length ch in
    System.String(Array.init len (fun _ -> ch.[rand.Next sz]))

let e = randomStr 5
printf "%s" e

I seem to keep receiving the following error and I have no clue as to why

error FS0010: Unexpected keyword 'let' or 'use' in expression. Expected 'in' or other token.

Upvotes: 1

Views: 265

Answers (1)

Brian Berns
Brian Berns

Reputation: 17038

I suspect you have lightweight syntax turned off via #light "off". I've reproduced the same error outside of Visual Studio here. Try removing the #light directive. There might also be some setting in VS2017 that disables lightweight syntax, but I don't have a copy handy that I can try. Lightweight syntax is now considered the norm in F#, so there's rarely any need for the old verbose style.

Upvotes: 1

Related Questions