F # limit value for seq <obj> but not list <obj>?

Error limiting value:

let myFn (s : string) (args : obj seq) = ()
let myOtherFn = myFn ""

Error limiting value:

let myFn (s : string) (args : obj list) = ()
let myOtherFn = myFn ""

Why?

+4
source share
1 answer

All bindings are subject to automatic generalization .

Since seq<'T>this is an interface (an alias for IEnumrable), the intended type for myOtherFnwill be val myOtherFn : ('_a -> unit) when '_a :> seq<obj>
that is common, but myOtherFn is not a function declaration (read the Value Constraint in the link above), therefore automatic generalization cannot deduce that it is the same as val myOtherFn : seq<obj> -> unit.

To force automatic generalization, you can add an explicit parameter to myOtherFn
let myOtherFn args = myFn "" args

+4
source

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


All Articles