简体   繁体   English

F#,小类型推断/注释错误

[英]F#, small type inference / annotation error

This line in F# gives the error that a type annotation may be needed, at "x.Day". F#中的这一行给出了可能需要在“ x.Day”处进行类型注释的错误。

  let daysList = List.map (fun x -> x.Day) datesList

But intellisense shows that x is of type DateTime (at "fun x"). 但是,Intellisense显示x的类型为DateTime(在“ fun x”处)。 And that datesList is of type DateTime list. 该dateList的类型为DateTime list。 So I'm perplexed as to why I have to declare the type of x like so and then everything works: 所以我很困惑为什么我必须这样声明x的类型,然后一切正常:

  let daysList = List.map (fun (x:System.DateTime) -> x.Day) datesList

The F# compiler generally prefers inferring types from left to right, so if you write it using the pipe operator, it works, assuming that datesList is a DateTime list : F#编译器通常更喜欢从左向右推断类型,因此,如果您使用管道运算符编写类型,则假定datesListDateTime list ,则它可以工作:

let daysList = datesList |> List.map (fun x -> x.Day)

Why, then, can IntelliSense determine that x is a DateTime ? 那么,为什么IntelliSense可以确定xDateTime Beats me, but in my experience it's fairly common that IntelliSense sometimes disagrees with the compiler. 击败我,但以我的经验,IntelliSense有时与编译器不同意是很普遍的。

BTW, that left to right rule doesn't always seem to be strict, either. 顺便说一句, 从左到右的规则也不总是很严格。 I often have to experiment a bit before I get everything right. 在正确解决所有问题之前,我经常需要做一些尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM