Reputation: 8127
I'm getting "Lookup on object of indeterminate type based on information prior to this program point" error on this snippet:
let a = [|"a"; "bb"|]
let n = Array.mapi (fun i x -> (i * x.Length)) a
What is wrong with it? Visual Studio F# Interactive correctly shows the type of x as string when I hover the cursor above it. Why do I have to write:
let a = [|"a"; "bb"|]
let n = Array.mapi (fun i (x:string) -> (i * x.Length)) a
to compile successfully?
Upvotes: 3
Views: 239
Reputation: 7735
The type checker works from left to right. Due to this reason, the compiler doesn't have enough information to infer a proper type for x
.
The easiest way to overcome this problem is to place a
in the beginning:
let n = a |> Array.mapi (fun i x -> (i * x.Length))
The compiler will know that a
has type of string []
, and therefore, x
is of string
.
Another alternative is using static functions:
let n = Array.mapi (fun i x -> (i * String.length x)) a
String.length
takes a string
, and so string
becomes an inferred type for x
.
Upvotes: 6