Why does this confuse F # compiler type inference?

There are no problems 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 Seq.randomis equal items:seq<'T> -> 'T. Things are good.

Yes, I know that I could just let random items = [...], this is not a question.

The thing is, it is itemssuddenly limited to the 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 am adding an object Randomas a closure. If I'm over Random, Intellisense shows me what the signature has become items:seq<obj> -> obj.

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

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

+4
source share
1

. , , , , . ( , random , - , )

. : let -, , , .

let x = [] - , nil [] . , let x = List.append [] [] , .

"" F # . F # : , - .., . , , .

, . ", , , , ".

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

let x = random [1;2;3]

, , , , "", - rnd . , :

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

let x = random() [1;2;3]
+8

Source: https://habr.com/ru/post/1681047/


All Articles