Question: I am trying to write an interpreter in F#. I want to check the type of expressions. 

Here is my discriminated union for the expressions

```
type Expr =
| Integer of int
| String of string
| Boolean of bool

```

This is the method i am using to check the types with

```
let checkType (e:Expr) =
match e with 
| String s -> s
| Integer i -> i
| Boolean b -> b

```

I want the method to determine wether an expression is a string,integer or boolean.

However, visual studio gives me the following error on line 4 of the checkType method:

```
 This expression was expected to have type string but here has type int

```

Am i missing something?

Answer: 
To expand on John Palmer's comment:

F# expects each function to have a single return type. For example, you can write a function that takes an int and returns an int, which would be a function of type `int -> int`. A function that parses strings into ints would be of type `string -> int`. And so on.

Now, what is the return type of the `checkType` function you've written? Since you don't specify a return type, the compiler looks at the type of the values you can return from the function -- every possible code branch must return a value, and they must all be the same type. So it looks at your `match` statement, sees that its first branch returns a `string`, and says, "Ah ha! I've figured out the return type of this function; this is a function that takes an `Expr` and returns a string. The function's type is `Expr -> string`."

Then it looks at the second branch of your `match` statement, and says, "Wait a minute. This is a function that returns a `string`, but in this code branch it's returning an `int`. That's not valid: the code that calls this function needs to know what type to expect it to return." And so you get the error.

Now, if you were to swap the order of your `match` statement cases, checking for `Integer i` first, then the compiler would evaluate your function as having type `Expr -> int` (taking an `Expr` input and returning an `int` output), and throw an error on the `| String s -> s` line. This time, the error would be "Wait a minute, this is a function that returns an `int`, so the expression `s` here should have been of type `int`. But instead, it's of type `string`. That's not valid."

Or delete the `| String s -> s` line, and you'll see an error "This expression was expected to have type `int` but here has type `bool`." Same thing: each function can have only one return type. If you want to return multiple different possible types from a function, that's what Discriminated Unions are for.

For more reading on F# types, see <http://fsharpforfunandprofit.com/series/understanding-fsharp-types.html>.
You can wrap all of the expressions into an Option type like Some or String:

```
let checkType (e:Expr) =
    match e with
    | String e -> "we are string: " + string e
    | Integer e-> "we are integer: " + string e
    | Boolean e -> "we are boolean: " + string e

```