0
votes
open System

[<EntryPoint>]
let main argv =
    match argv with
        | [| firstArg |] -> printfn "Your first arg is %s", firstArg
        | [| |] -> failwith "You didn't pass an argument"
        | _ -> failwith "You did something unusual"
    0 // return an integer exit code

I wrote this to process the first argument to my F# console application. If I didn't pass an argument it fails with an exception saying "You didn't pass an argument". If I passed at least two arguments, it fails with an exception "You did something unusual". But, when I pass exactly one argument, it tells nothing. Why does not printfn work here?

1
The first case will only match if there is exactly one item in the array. - TheQuickBrownFox

1 Answers

3
votes

The reason it didn't print anything here is because you've added an extra comma after the printf. That means the signature is a string -> unit function and string tuple. If you remove the comma then it will work.

A working solution would be


[<EntryPoint>]
let main argv =
    match argv with
        | [| firstArg |] -> printfn "Your first arg is %s" firstArg
        | [| |] -> failwith "You didn't pass an argument"
        | _ -> failwith "You did something unusual"
    0 // return an integer exit code

You might have seen a compiler warning before running this which said warning FS0020: The result of this expression has type '(string -> unit) * string' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'