F# Type Inference -


i kind of new f# maybe question dumb. wrote program in f# used generic types. compiler determined types not way desired because had bug in deepest function call had instantiated type wrong type. resulted in type mismatch elsewhere had used type had expected. have mention find root of problem tried explicitly enforce higher level functions use types desired generic type. type mismatch shown in high-level functions not low level function type instantiated. think it's not convenient way of determining types because easier programmers determine type on higher level functions , explicit type assignment should result in type error in lower level function type has been determined. in experience seems automatic type determination of compiler overrides explicit type declaration. understanding wrong here?

something code below:

type test<'a,'b>={func:'a->'b; arg:'a}  let c(a)=   a.func(2)|>ignore  let b(a)=   c(a)  let a(a:test<int64,int64>)=   b(a) //<-----------------------the type mismatch detected here 

having error detected on such high level function call makes difficult find root of problem because not have bugs regarding value of variables type determination bugs.

f#'s type inference (fairly) strict top-to-bottom, left-to-right. see here discussion: why f#'s type inference fickle?

you can check rearranging code (just demonstration):

type test<'a,'b>={func:'a->'b; arg:'a}  let rec b(a)=   c(a)  , a(a:test<int64,int64>)=   b(a)  , c(a)=   a.func(2)|>ignore   // type mismatch here 

the convenience level depends on concrete code, in experience. have admit have been surprised type mismatch error messages. takes time getting used to.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -