Why do I need annotation here?

In the following code:

type ParseResult<'a> =
    {
        Result : Option<'a>;
        Rest : string
    }

type Parser<'a> = string -> ParseResult<'a>

let ThenBind p (f : Option<'a> -> Parser<'b>) : Parser<'b> =
    fun input ->
        let r = p input
        match r.Result with
        | None -> { Result = None; Rest = input }
        | _ -> (f r.Result) r.Rest

      

With type annotation for f, the type for ThenBind is:

p:(string -> ParseResult<'a>) ->
f:(Option<'a> -> Parser<'b>) ->
input:string -> ParseResult<'b>

      

But without annotation, this:

p:(string -> ParseResult<'a>) ->
f:(Option<'a> -> string -> ParseResult<'b>) ->
input:string -> ParseResult<'b>

      

Why?

+3


source to share


1 answer


You don't need type annotation. The two types are identical.



Parser<'a>

is just an alias string -> ParseResult<'a>

, so it doesn't matter if the result type is declared f

as Parser<'b>

or string -> ParseResult<'b>

. They are exactly the same.

+8


source







All Articles