References don't work?
I just started with LINQPad 5 and I am not very experienced (to say the least) with .NET. I have the following F# script that runs in Visual Studio:
open System
open System.Collections
let x = [1 .. 10]
let enumerator = x.GetEnumerator()
while enumerator.MoveNext() do
let num = enumerator.Current
printfn "%i" num
LINQPad complains about the first line, says Unmatched '('.
I tried to add the references using the F4 key. The two DLLs show in the Query Properties window that pops up. After removing the two open statements I still get an error, with the message:
The field constructor or member 'GetEnumerator' is not defined (using external F# compiler)
What am I doing wrong?
open System
open System.Collections
let x = [1 .. 10]
let enumerator = x.GetEnumerator()
while enumerator.MoveNext() do
let num = enumerator.Current
printfn "%i" num
LINQPad complains about the first line, says Unmatched '('.
I tried to add the references using the F4 key. The two DLLs show in the Query Properties window that pops up. After removing the two open statements I still get an error, with the message:
The field constructor or member 'GetEnumerator' is not defined (using external F# compiler)
What am I doing wrong?
Comments
[1..10]
) to anIEnumerable
like this:let x = [1 .. 10] :> IEnumerable
. BecauseIEnumerable
is a non-generic enumerator interface, theCurrent
property returns an object that you need to cast back to an integer (i.e.let num = enumerator.Current :?> int
) because that's whatprintfn "%i"
expects. Here is the fixed code: And here it is running in another F# Interactive session: Back in LINQPad, make sure the query language is set to F# Program and then you should be able to just run the same code: The `open` import declarations are harmless and can be omitted in LINQPad because I think it does that for you for quite a few of the commonly usedSystem
namespaces.The fixed code runs within a project or in fsi, but only if the two open statements are included.
Many thanks for your help. I thought a list would be automatically enumerable, and in hindsight it is clear that casting Current to int is necessary for the print statement to work.
I tried static casting (enumerator.Current :> int, "?" omitted) and it did not work. Being a beginner (three days with F#) I still do not know why.