Why does this confuse the F# compiler's type inference?

Viewed 61

No problem here:

module Seq =
    let private rnd = Random Environment.TickCount

    let random =
        fun (items : 'T seq) ->
            let count = Seq.length items
            items |> Seq.nth (rnd.Next count)

The signature of Seq.random is items:seq<'T> -> 'T. All good.

Yes, I know that I could just let random items = [...], that is not the point.

The point is that items is suddenly constrained to be type seq<obj> when I do this:

module Seq =
    let random =
        let rnd = Random Environment.TickCount
        fun (items : 'T seq) ->
            let count = Seq.length items
            items |> Seq.nth (rnd.Next count)

... i.e. I add the Random object as a closure. If I hover over random, Intellisense shows me that the signature has become items:seq<obj> -> obj.

Interestingly, if I select the code and hit [Alt]+[Enter] to execute it in F# Interactive, the signature shows as seq<'a> -> 'a. WTH??

So, what's going on, here? Why the confusion and inconsistency in type inference?

1 Answers
Related