layout: page
title: “Troubleshooting F#”
description: “Why won’t my code compile?”
nav: troubleshooting-fsharp

hasComments: 1

As the saying goes, “if it compiles, it’s correct”, but it can be extremely frustrating just trying to get the code to compile at all! So this page is devoted to helping you troubleshoot your F# code.

I will first present some general advice on troubleshooting and some of the most common errors that beginners make. After that, I will describe each of the common error messages in detail, and give examples of how they can occur and how to correct them.

(Jump to the error numbers)

General guidelines for troubleshooting

By far the most important thing you can do is to take the time and effort to understand exactly how F# works, especially the core concepts involving functions and the type system. So please read and reread the series “thinking functionally” and “understanding F# types”, play with the examples, and get comfortable with the ideas before you try to start doing serious coding. If you don’t understand how functions and types work, then the compiler errors will not make any sense.

If you are coming from an imperative language such as C#, you may have developed some bad habits by relying on the debugger to find and fix incorrect code. In F#, you will probably not get that far, because the compiler is so much stricter in many ways. And of course, there is no tool to “debug” the compiler and step through its processing. The best tool for debugging compiler errors is your brain, and F# forces you to use it!

Nevertheless, there are a number of extremely common errors that beginners make, and I will quickly go through them.

Don’t use parentheses when calling a function

In F#, whitespace is the standard separator for function parameters. You will rarely need to use parentheses, and in particular, do not use parentheses when calling a function.

  1. let add x y = x + y
  2. let result = add (1 2) //wrong
  3. // error FS0003: This value is not a function and cannot be applied
  4. let result = add 1 2 //correct

Don’t mix up tuples with multiple parameters

If it has a comma, it is a tuple. And a tuple is one object not two. So you will get errors about passing the wrong type of parameter, or too few parameters.

  1. addTwoParams (1,2) // trying to pass a single tuple rather than two args
  2. // error FS0001: This expression was expected to have type
  3. // int but here has type 'a * 'b

The compiler treats (1,2) as a generic tuple, which it attempts to pass to “addTwoParams“. Then it complains that the first parameter of addTwoParams is an int, and we’re trying to pass a tuple.

If you attempt to pass two arguments to a function expecting one tuple, you will get another obscure error.

  1. addTuple 1 2 // trying to pass two args rather than one tuple
  2. // error FS0003: This value is not a function and cannot be applied

Watch out for too few or too many arguments

The F# compiler will not complain if you pass too few arguments to a function (in fact “partial application” is an important feature), but if you don’t understand what is going on, you will often get strange “type mismatch” errors later.

Similarly the error for having too many arguments is typically “This value is not a function” rather than a more straightforward error.

The “printf” family of functions is very strict in this respect. The argument count must be exact.

This is a very important topic ? it is critical that you understand how partial application works. See the series “thinking functionally” for a more detailed discussion.

Use semicolons for list separators

In the few places where F# needs an explicit separator character, such as lists and records, the semicolon is used. Commas are never used. (Like a broken record, I will remind you that commas are for tuples).

  1. let list1 = [1,2,3] // wrong! This is a ONE-element list containing
  2. // a three-element tuple
  3. let list1 = [1;2;3] // correct
  4. type Customer = {Name:string, Address: string} // wrong
  5. type Customer = {Name:string; Address: string} // correct

Don’t use ! for not or != for not-equal

The exclamation point symbol is not the “NOT” operator. It is the deferencing operator for mutable references. If you use it by mistake, you will get the following error:

  1. let y = true
  2. let z = !y
  3. // => error FS0001: This expression was expected to have
  4. // type 'a ref but here has type bool

The correct construction is to use the “not” keyword. Think SQL or VB syntax rather than C syntax.

  1. let y = true
  2. let z = not y //correct

And for “not equal”, use “<>”, again like SQL or VB.

  1. let z = 1 <> 2 //correct

Don’t use = for assignment

If you are using mutable values, the assignment operation is written “<-“. If you use the equals symbol you might not even get an error, just an unexpected result.

  1. let mutable x = 1
  2. x = x + 1 // returns false. x is not equal to x+1
  3. x <- x + 1 // assigns x+1 to x

Watch out for hidden tab characters

The indenting rules are very straightforward, and it is easy to get the hang of them. But you are not allowed to use tabs, only spaces.

  1. let add x y =
  2. {tab}x + y
  3. // => error FS1161: TABs are not allowed in F# code

Be sure to set your editor to convert tabs to spaces. And watch out if you are pasting code in from elsewhere. If you do run into persistent problems with a bit of code, try removing the whitespace and re-adding it.

Don’t mistake simple values for function values

If you are trying to create a function pointer or delegate, watch out that you don’t accidentally create a simple value that has already been evaluated.

If you want a parameterless function that you can reuse, you will need to explicitly pass a unit parameter, or define it as a lambda.

  1. let reader = new System.IO.StringReader("hello")
  2. let nextLineFn = reader.ReadLine() //wrong
  3. let nextLineFn() = reader.ReadLine() //correct
  4. let nextLineFn = fun() -> reader.ReadLine() //correct
  5. let r = new System.Random()
  6. let randomFn = r.Next() //wrong
  7. let randomFn() = r.Next() //correct
  8. let randomFn = fun () -> r.Next() //correct

See the series “thinking functionally” for more discussion of parameterless functions.

Tips for troubleshooting “not enough information” errors

The F# compiler is currently a one-pass left-to-right compiler, and so type information later in the program is unavailable to the compiler if it hasn’t been parsed yet.

A number of errors can be caused by this, such as “FS0072: Lookup on object of indeterminate type” and “FS0041: A unique overload for could not be determined”. The suggested fixes for each of these specific cases are described below, but there are some general principles that can help if the compiler is complaining about missing types or not enough information. These guidelines are:

  • Define things before they are used (this includes making sure the files are compiled in the right order)
  • Put the things that have “known types” earlier than things that have “unknown types”. In particular, you might be able reorder pipes and similar chained functions so that the typed objects come first.
  • Annotate as needed. One common trick is to add annotations until everything works, and then take them away one by one until you have the minimum needed.

Do try to avoid annotating if possible. Not only is it not aesthetically pleasing, but it makes the code more brittle. It is a lot easier to change types if there are no explicit dependencies on them.

Here is a list of the major errors that seem to me worth documenting. I have not documented any errors that are self explanatory, only those that seem obscure to beginners.

I will continue to add to the list in the future, and I welcome any suggestions for additions.

FS0001: The type ‘X’ does not match the type ‘Y’

This is probably the most common error you will run into. It can manifest itself in a wide variety of contexts, so I have grouped the most common problems together with examples and fixes. Do pay attention to the error message, as it is normally quite explicit about what the problem is.






























































Error message Possible causes
The type ‘float’ does not match the type ‘int’ A. Can’t mix floats and ints
The type ‘int’ does not support any operators named ‘DivideByInt’ A. Can’t mix floats and ints.
The type ‘X’ is not compatible with any of the types B. Using the wrong numeric type.
This type (function type) does not match the type (simple type). Note: function types have a arrow in them, like ‘a -> ‘b. C. Passing too many arguments to a function.
This expression was expected to have (function type) but here has (simple type) C. Passing too many arguments to a function.
This expression was expected to have (N part function) but here has (N-1 part function) C. Passing too many arguments to a function.
This expression was expected to have (simple type) but here has (function type) D. Passing too few arguments to a function.
This expression was expected to have (type) but here has (other type) E. Straightforward type mismatch.

F. Inconsistent returns in branches or matches.

G. Watch out for type inference effects buried in a function.

Type mismatch. Expecting a (simple type) but given a (tuple type). Note: tuple types have a star in them, like ‘a ‘b. H. Have you used a comma instead of space or semicolon?
Type mismatch. Expecting a (tuple type) but given a (different tuple type). I. Tuples must be the same type to be compared.
This expression was expected to have type ‘a ref but here has type X J. Don’t use ! as the “not” operator.
The type (type) does not match the type (other type) K. Operator precedence (especially functions and pipes).
This expression was expected to have type (monadic type) but here has type ‘b ‘c L. let! error in computation expressions.

A. Can’t mix ints and floats

Unlike C# and most imperative languages, ints and floats cannot be mixed in expressions. You will get a type error if you attempt this:

  1. 1 + 2.0 //wrong
  2. // => error FS0001: The type 'float' does not match the type 'int'

The fix is to cast the int into a float first:

  1. float 1 + 2.0 //correct

This issue can also manifest itself in library functions and other places. For example, you cannot do “average“ on a list of ints.

  1. [1..10] |> List.average // wrong
  2. // => error FS0001: The type 'int' does not support any
  3. // operators named 'DivideByInt'

You must cast each int to a float first, as shown below:

  1. [1..10] |> List.map float |> List.average //correct
  2. [1..10] |> List.averageBy float //correct (uses averageBy)

B. Using the wrong numeric type

You will get a “not compatible” error when a numeric cast failed.

  1. printfn "hello %i" 1.0 // should be a int not a float
  2. // error FS0001: The type 'float' is not compatible
  3. // with any of the types byte,int16,int32...

One possible fix is to cast it if appropriate.

  1. printfn "hello %i" (int 1.0)

C. Passing too many arguments to a function

  1. let add x y = x + y
  2. let result = add 1 2 3
  3. // ==> error FS0001: The type ''a -> 'b' does not match the type 'int'

The clue is in the error.

The fix is to remove one of the arguments!

Similar errors are caused by passing too many arguments to printf.

  1. printfn "hello" 42
  2. // ==> error FS0001: This expression was expected to have type 'a -> 'b
  3. // but here has type unit
  4. printfn "hello %i" 42 43
  5. // ==> Error FS0001: Type mismatch. Expecting a 'a -> 'b -> 'c
  6. // but given a 'a -> unit
  7. printfn "hello %i %i" 42 43 44
  8. // ==> Error FS0001: Type mismatch. Expecting a 'a -> 'b -> 'c -> 'd
  9. // but given a 'a -> 'b -> unit

D. Passing too few arguments to a function

If you do not pass enough arguments to a function, you will get a partial application. When you later use it, you get an error because it is not a simple type.

  1. let reader = new System.IO.StringReader("hello");
  2. let line = reader.ReadLine //wrong but compiler doesn't complain
  3. printfn "The line is %s" line //compiler error here!
  4. // ==> error FS0001: This expression was expected to have type string
  5. // but here has type unit -> string

This is particularly common for some .NET library functions that expect a unit parameter, such as ReadLine above.

The fix is to pass the correct number of parameters. Check the type of the result value to make sure that it is indeed a simple type. In the ReadLine case, the fix is to pass a () argument.

  1. let line = reader.ReadLine() //correct
  2. printfn "The line is %s" line //no compiler error

E. Straightforward type mismatch

The simplest case is that you have the wrong type, or you are using the wrong type in a print format string.

  1. printfn "hello %s" 1.0
  2. // => error FS0001: This expression was expected to have type string
  3. // but here has type float

F. Inconsistent return types in branches or matches

A common mistake is that if you have a branch or match expression, then every branch MUST return the same type. If not, you will get a type error.

  1. let f x =
  2. if x > 1 then "hello"
  3. else 42
  4. // => error FS0001: This expression was expected to have type string
  5. // but here has type int
  1. let g x =
  2. match x with
  3. | 1 -> "hello"
  4. | _ -> 42
  5. // error FS0001: This expression was expected to have type
  6. // string but here has type int

Obviously, the straightforward fix is to make each branch return the same type.

  1. let f x =
  2. if x > 1 then "hello"
  3. else "42"
  4. let g x =
  5. match x with
  6. | 1 -> "hello"
  7. | _ -> "42"

Remember that if an “else” branch is missing, it is assumed to return unit, so the “true” branch must also return unit.

  1. let f x =
  2. if x > 1 then "hello"
  3. // error FS0001: This expression was expected to have type
  4. // unit but here has type string

If both branches cannot return the same type, you may need to create a new union type that can contain both types.

  1. type StringOrInt = | S of string | I of int // new union type
  2. let f x =
  3. if x > 1 then S "hello"
  4. else I 42

G. Watch out for type inference effects buried in a function

A function may cause an unexpected type inference that ripples around your code. For example, in the following, the innocent print format string accidentally causes doSomething to expect a string.

  1. let doSomething x =
  2. // do something
  3. printfn "x is %s" x
  4. // do something more
  5. doSomething 1
  6. // => error FS0001: This expression was expected to have type string
  7. // but here has type int

The fix is to check the function signatures and drill down until you find the guilty party. Also, use the most generic types possible, and avoid type annotations if possible.

H. Have you used a comma instead of space or semicolon?

If you are new to F#, you might accidentally use a comma instead of spaces to separate function arguments:

  1. // define a two parameter function
  2. let add x y = x + 1
  3. add(x,y) // FS0001: This expression was expected to have
  4. // type int but here has type 'a * 'b

The fix is: don’t use a comma!

  1. add x y // OK

One area where commas are used is when calling .NET library functions.
These all take tuples as arguments, so the comma form is correct. In fact, these calls look just the same as they would from C#:

  1. // correct
  2. System.String.Compare("a","b")
  3. // incorrect
  4. System.String.Compare "a" "b"

I. Tuples must be the same type to be compared or pattern matched

Tuples with different types cannot be compared. Trying to compare a tuple of type int * int, with a tuple of type int * string results in an error:

  1. let t1 = (0, 1)
  2. let t2 = (0, "hello")
  3. t1 = t2
  4. // => error FS0001: Type mismatch. Expecting a int * int
  5. // but given a int * string
  6. // The type 'int' does not match the type 'string'

And the length must be the same:

  1. let t1 = (0, 1)
  2. let t2 = (0, 1, "hello")
  3. t1 = t2
  4. // => error FS0001: Type mismatch. Expecting a int * int
  5. // but given a int * int * string
  6. // The tuples have differing lengths of 2 and 3

You can get the same issue when pattern matching tuples during binding:

  1. let x,y = 1,2,3
  2. // => error FS0001: Type mismatch. Expecting a 'a * 'b
  3. // but given a 'a * 'b * 'c
  4. // The tuples have differing lengths of 2 and 3
  5. let f (x,y) = x + y
  6. let z = (1,"hello")
  7. let result = f z
  8. // => error FS0001: Type mismatch. Expecting a int * int
  9. // but given a int * string
  10. // The type 'int' does not match the type 'string'

J. Don’t use ! as the “not” operator

If you use ! as a “not” operator, you will get a type error mentioning the word “ref”.

  1. let y = true
  2. let z = !y //wrong
  3. // => error FS0001: This expression was expected to have
  4. // type 'a ref but here has type bool

The fix is to use the “not” keyword instead.

  1. let y = true
  2. let z = not y //correct

K. Operator precedence (especially functions and pipes)

If you mix up operator precedence, you may get type errors. Generally, function application is highest precedence compared to other operators, so you get an error in the case below:

  1. String.length "hello" + "world"
  2. // => error FS0001: The type 'string' does not match the type 'int'
  3. // what is really happening
  4. (String.length "hello") + "world"

The fix is to use parentheses.

  1. String.length ("hello" + "world") // corrected

Conversely, the pipe operator is low precedence compared to other operators.

  1. let result = 42 + [1..10] |> List.sum
  2. // => => error FS0001: The type ''a list' does not match the type 'int'
  3. // what is really happening
  4. let result = (42 + [1..10]) |> List.sum

Again, the fix is to use parentheses.

  1. let result = 42 + ([1..10] |> List.sum)

L. let! error in computation expressions (monads)

Here is a simple computation expression:

  1. type Wrapper<'a> = Wrapped of 'a
  2. type wrapBuilder() =
  3. member this.Bind (wrapper:Wrapper<'a>) (func:'a->Wrapper<'b>) =
  4. match wrapper with
  5. | Wrapped(innerThing) -> func innerThing
  6. member this.Return innerThing =
  7. Wrapped(innerThing)
  8. let wrap = new wrapBuilder()

However, if you try to use it, you get an error.

  1. wrap {
  2. let! x1 = Wrapped(1) // <== error here
  3. let! y1 = Wrapped(2)
  4. let z1 = x + y
  5. return z
  6. }
  7. // error FS0001: This expression was expected to have type Wrapper<'a>
  8. // but here has type 'b * 'c

The reason is that “Bind“ expects a tuple (wrapper,func), not two parameters. (Check the signature for bind in the F# documentation).

The fix is to change the bind function to accept a tuple as its (single) parameter.

  1. type wrapBuilder() =
  2. member this.Bind (wrapper:Wrapper<'a>, func:'a->Wrapper<'b>) =
  3. match wrapper with
  4. | Wrapped(innerThing) -> func innerThing

FS0003: This value is not a function and cannot be applied

This error typically occurs when passing too many arguments to a function.

  1. let add1 x = x + 1
  2. let x = add1 2 3
  3. // ==> error FS0003: This value is not a function and cannot be applied

It can also occur when you do operator overloading, but the operators cannot be used as prefix or infix.

  1. let (!!) x y = x + y
  2. (!!) 1 2 // ok
  3. 1 !! 2 // failed !! cannot be used as an infix operator
  4. // error FS0003: This value is not a function and cannot be applied

FS0008: This runtime coercion or type test involves an indeterminate type

You will often see this when attempting to use “:?“ operator to match on a type.

  1. let detectType v =
  2. match v with
  3. | :? int -> printfn "this is an int"
  4. | _ -> printfn "something else"
  5. // error FS0008: This runtime coercion or type test from type 'a to int
  6. // involves an indeterminate type based on information prior to this program point.
  7. // Runtime type tests are not allowed on some types. Further type annotations are needed.

The message tells you the problem: “runtime type tests are not allowed on some types”.

The answer is to “box” the value which forces it into a reference type, and then you can type check it:

  1. let detectTypeBoxed v =
  2. match box v with // used "box v"
  3. | :? int -> printfn "this is an int"
  4. | _ -> printfn "something else"
  5. //test
  6. detectTypeBoxed 1
  7. detectTypeBoxed 3.14

FS0010: Unexpected identifier in binding

Typically caused by breaking the “offside” rule for aligning expressions in a block.

  1. //3456789
  2. let f =
  3. let x=1 // offside line is at column 3
  4. x+1 // oops! don't start at column 4
  5. // error FS0010: Unexpected identifier in binding

The fix is to align the code correctly!

See also FS0588: Block following this ‘let’ is unfinished for another issue caused by alignment.

FS0010: Incomplete structured construct

Often occurs if you are missing parentheses from a class constructor:

  1. type Something() =
  2. let field = ()
  3. let x1 = new Something // Error FS0010
  4. let x2 = new Something() // OK!

Can also occur if you forgot to put parentheses around an operator:

  1. // define new operator
  2. let (|+) a = -a
  3. |+ 1 // error FS0010:
  4. // Unexpected infix operator
  5. (|+) 1 // with parentheses -- OK!

Can also occur if you are missing one side of an infix operator:

  1. || true // error FS0010: Unexpected symbol '||'
  2. false || true // OK

Can also occur if you attempt to send a namespace definition to F# interactive. The interactive console does not allow namespaces.

  1. namespace Customer // FS0010: Incomplete structured construct
  2. // declare a type
  3. type Person= {First:string; Last:string}

FS0013: The static coercion from type X to Y involves an indeterminate type

This is generally caused by implic

FS0020: This expression should have type ‘unit’

This error is commonly found in two situations:

  • Expressions that are not the last expression in the block
  • Using wrong assignment operator

FS0020 with expressions that are not the last expression in the block

Only the last expression in a block can return a value. All others must return unit. So this typically occurs when you have a function in a place that is not the last function.

  1. let something =
  2. 2+2 // => FS0020: This expression should have type 'unit'
  3. "hello"

The easy fix is use ignore. But ask yourself why you are using a function and then throwing away the answer ? it might be a bug.

  1. let something =
  2. 2+2 |> ignore // ok
  3. "hello"

This also occurs if you think you writing C# and you accidentally use semicolons to separate expressions:

  1. // wrong
  2. let result = 2+2; "hello";
  3. // fixed
  4. let result = 2+2 |> ignore; "hello";

FS0020 with assignment

Another variant of this error occurs when assigning to a property.

  1. This expression should have type 'unit', but has type 'Y'.

With this error, chances are you have confused the assignment operator “<-“ for mutable values, with the equality comparison operator “=“.

  1. // '=' versus '<-'
  2. let add() =
  3. let mutable x = 1
  4. x = x + 1 // warning FS0020
  5. printfn "%d" x

The fix is to use the proper assignment operator.

  1. // fixed
  2. let add() =
  3. let mutable x = 1
  4. x <- x + 1
  5. printfn "%d" x

FS0030: Value restriction

This is related to F#’s automatic generalization to generic types whenever possible.

For example, given :

  1. let id x = x
  2. let compose f g x = g (f x)
  3. let opt = None

F#’s type inference will cleverly figure out the generic types.

  1. val id : 'a -> 'a
  2. val compose : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c
  3. val opt : 'a option

However in some cases, the F# compiler feels that the code is ambiguous, and, even though it looks like it is guessing the type correctly, it needs you to be more specific:

  1. let idMap = List.map id // error FS0030
  2. let blankConcat = String.concat "" // error FS0030

Almost always this will be caused by trying to define a partially applied function, and almost always, the easiest fix is to explicitly add the missing parameter:

  1. let idMap list = List.map id list // OK
  2. let blankConcat list = String.concat "" list // OK

For more details see the MSDN article on “automatic generalization”.

FS0035: This construct is deprecated

F# syntax has been cleaned up over the last few years, so if you are using examples from an older F# book or webpage, you may run into this. See the MSDN documentation for the correct syntax.

  1. let x = 10
  2. let rnd1 = System.Random x // Good
  3. let rnd2 = new System.Random(x) // Good
  4. let rnd3 = new System.Random x // error FS0035

FS0039: The field, constructor or member X is not defined

This error is commonly found in four situations:

  • The obvious case where something really isn’t defined! And make sure that you don’t have a typo or case mismatch either.
  • Interfaces
  • Recursion
  • Extension methods

FS0039 with interfaces

In F# all interfaces are “explicit” implementations rather than “implicit”. (Read the C# documentation on “explicit interface implementation” for an explanation of the difference).

The key point is that when a interface member is explicitly implemented, it cannot be accessed through a normal class instance, but only through an instance of the interface, so you have to cast to the interface type by using the :> operator.

Here’s an example of a class that implements an interface:

  1. type MyResource() =
  2. interface System.IDisposable with
  3. member this.Dispose() = printfn "disposed"

This doesn’t work:

  1. let x = new MyResource()
  2. x.Dispose() // error FS0039: The field, constructor
  3. // or member 'Dispose' is not defined

The fix is to cast the object to the interface, as below:

  1. // fixed by casting to System.IDisposable
  2. (x :> System.IDisposable).Dispose() // OK
  3. let y = new MyResource() :> System.IDisposable
  4. y.Dispose() // OK

FS0039 with recursion

Here’s a standard Fibonacci implementation:

  1. let fib i =
  2. match i with
  3. | 1 -> 1
  4. | 2 -> 1
  5. | n -> fib(n-1) + fib(n-2)

Unfortunately, this will not compile:

  1. Error FS0039: The value or constructor 'fib' is not defined

The reason is that when the compiler sees ‘fib’ in the body, it doesn’t know about the function because it hasn’t finished compiling it yet!

The fix is to use the “rec“ keyword.

  1. let rec fib i =
  2. match i with
  3. | 1 -> 1
  4. | 2 -> 1
  5. | n -> fib(n-1) + fib(n-2)

Note that this only applies to “let“ functions. Member functions do not need this, because the scope rules are slightly different.

  1. type FibHelper() =
  2. member this.fib i =
  3. match i with
  4. | 1 -> 1
  5. | 2 -> 1
  6. | n -> fib(n-1) + fib(n-2)

FS0039 with extension methods

If you have defined an extension method, you won’t be able to use it unless the module is in scope.

Here’s a simple extension to demonstrate:

  1. module IntExtensions =
  2. type System.Int32 with
  3. member this.IsEven = this % 2 = 0

If you try to use it the extension, you get the FS0039 error:

  1. let i = 2
  2. let result = i.IsEven
  3. // FS0039: The field, constructor or
  4. // member 'IsEven' is not defined

The fix is just to open the IntExtensions module.

  1. open IntExtensions // bring module into scope
  2. let i = 2
  3. let result = i.IsEven // fixed!

FS0041: A unique overload for could not be determined

This can be caused when calling a .NET library function that has multiple overloads:

  1. let streamReader filename = new System.IO.StreamReader(filename) // FS0041

There a number of ways to fix this. One way is to use an explicit type annotation:

  1. let streamReader filename = new System.IO.StreamReader(filename:string) // OK

You can sometimes use a named parameter to avoid the type annotation:

  1. let streamReader filename = new System.IO.StreamReader(path=filename) // OK

Or you can try to create intermediate objects that help the type inference, again without needing type annotations:

  1. let streamReader filename =
  2. let fileInfo = System.IO.FileInfo(filename)
  3. new System.IO.StreamReader(fileInfo.FullName) // OK

FS0049: Uppercase variable identifiers should not generally be used in patterns

When pattern matching, be aware of a subtle difference between the pure F# union types which consist of a tag only, and a .NET Enum type.

Pure F# union type:

  1. type ColorUnion = Red | Yellow
  2. let redUnion = Red
  3. match redUnion with
  4. | Red -> printfn "red" // no problem
  5. | _ -> printfn "something else"

But with .NET enums you must fully qualify them:

  1. type ColorEnum = Green=0 | Blue=1 // enum
  2. let blueEnum = ColorEnum.Blue
  3. match blueEnum with
  4. | Blue -> printfn "blue" // warning FS0049
  5. | _ -> printfn "something else"

The fixed version:

  1. match blueEnum with
  2. | ColorEnum.Blue -> printfn "blue"
  3. | _ -> printfn "something else"

FS0072: Lookup on object of indeterminate type

This occurs when “dotting into” an object whose type is unknown.

Consider the following example:

  1. let stringLength x = x.Length // Error FS0072

The compiler does not know what type “x” is, and therefore does not know if “Length“ is a valid method.

There a number of ways to fix this. The crudest way is to provide an explicit type annotation:

  1. let stringLength (x:string) = x.Length // OK

In some cases though, judicious rearrangement of the code can help. For example, the example below looks like it should work. It’s obvious to a human that the List.map function is being applied to a list of strings, so why does x.Length cause an error?

  1. List.map (fun x -> x.Length) ["hello"; "world"] // Error FS0072

The reason is that the F# compiler is currently a one-pass compiler, and so type information present later in the program cannot be used if it hasn’t been parsed yet.

Yes, you can always explicitly annotate:

  1. List.map (fun x:string -> x.Length) ["hello"; "world"] // OK

But another, more elegant way that will often fix the problem is to rearrange things so the known types come first, and the compiler can digest them before it moves to the next clause.

  1. ["hello"; "world"] |> List.map (fun x -> x.Length) // OK

It’s good practice to avoid explicit type annotations, so this approach is best, if it is feasible.

FS0588: Block following this ‘let’ is unfinished

Caused by outdenting an expression in a block, and thus breaking the “offside rule”.

  1. //3456789
  2. let f =
  3. let x=1 // offside line is at column 3
  4. x+1 // offside! You are ahead of the ball!
  5. // error FS0588: Block following this
  6. // 'let' is unfinished

The fix is to align the code correctly.

See also FS0010: Unexpected identifier in binding for another issue caused by alignment.