Carbon
Carbon

Reputation: 3943

Why is there a type error in this f# expression?

first day working in F#. I've spent some time in Haskell and am trying to learn F# to work with some existing .net code. Why is this code angry with me? What's the equivalent to :t? How would I add a type annotation to main?

type Color = Red | Green
[<EntryPoint>]
let main = 
    let a = Red
    if a = Red then
       printfn "hi!"
     else
       printfn "no!"

Upvotes: 1

Views: 84

Answers (1)

DaveShaw
DaveShaw

Reputation: 52798

The signature of the function with an [<EntryPoint>] attribute (e.g. main) should be string[] -> int in your version it is missing the string[] parameter and a return value of type int

You can remedy it by adding those in:

type Color = Red | Green
[<EntryPoint>]
let main argv = //argv added here is inferred to be string[]
    let a = Red
    if a = Red then
       printfn "hi!"
     else
       printfn "no!"
    0 //Return 0, all OK

Without the 0 at the end to return an int, you are returning unit (the result of printfn).

Upvotes: 6

Related Questions