开发者

F# pattern matching problem?

I have a problem when pattern matching with F#. I'm building an F# library and have this so far:

namespace parser
    module parse =
        let public y = function 
        | x when x.Contains("开发者_运维技巧hi") -> "HELLO" 
        | x when x.Contains("hello") -> "HI" 
        | x -> x

but it gives me error: Error Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to constrain the type of the object. This may allow the lookup to be resolved.


Essentially, the compiler doesn't understand that you want the argument to your function to be of type string. Basically, you can NOT invoke instance methods on implicitly declared function arguments. You'll need to use something like the following.

let y (value:string) = 
  match value when
  | x when x.Contains("hi") -> "HELLO" 
  | x when x.Contains("hello") -> "HI" 
  | x -> x

Interestingly, if you were not invoking an instance method, but instead using it in some other way (where the type was already known), you would be fine. In other words, suppose a StringUtils module contained a method Has, which took in two strings and performed the same check as Contains; if so, you could use the implicit argument, because the compiler already knows of which type the value must be.

  module StringUtils =
    let has (word:string) value = word.Contains(value)

  module Parse =
    let y = function 
     | x when x |> StringUtils.has "hi" -> "HELLO" 
     | x when x |> StringUtils.has "hello" -> "HI" 
     | x -> x

Obviously, in many cases, this sort of thing is unnecessarily verbose. But it demonstrates the larger point about F#'s type inference behavior.


The compiler wants to know the type of x is in your y function so you need to annotate it, like this:

let y (x: string) =
  match x with
  | x when x.Contains "hi" -> "HELLO"
  | x when x.Contains "hello" -> "HI"
  | x -> x
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜