1
votes

I have a function that returns the char at a location:

let NthChar inpStr indxNum = 
    if inpStr.Length >=1 then printfn "Character %s" inpStr.[indxNum];
    else printfn "Not enough arguments"

Error for

inpStr.Length 

and

inpStr.[indxNum]

Error trace:

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.

1
you're already using the char from the string at the given index in your code, so what is the problem ? - Sehnsucht
Updated question, sorry. - user6911980
You're going to have to add a type annotation. - Mark Seemann
Or use the String module's String.length function instead of the String type's Length property. - ildjarn
Do you want that?open System let NthChar inpStr indxNum = if inpStr |> String.IsNullOrWhiteSpace || indxNum > (inpStr |> String.length) then printfn "Not enough arguments" else printfn "Character %A" inpStr.[indxNum] - FoggyFinder

1 Answers

2
votes

You're trying to "dot into" something and access it's indexer. Except that when you do this the type of that something is still unknown (maybe it doesn't have an indexer)
So you got that rather explicit error message which also gives you the way to remove it, simply add a type annotation :

// code shortened
let nthChar (inpStr : string) indxNum = inpStr.[indxNum]

// alternative syntax
let nthChar inpStr indxNum = (inpStr : string).[indxNum]