Hide this comment

Empty list is polymorphic, but an explicit type annotation should do the trick:

[] : int list :> IList<int >

By on 6/23/2009 11:24 AM ()Reply
Hide this comment

My understanding is [] in F# is quite 'internal' to F#.

This should do it

let x = (Array.of_list [1..1]) :> System.Collections.Generic.IList<int>

By on 6/22/2009 7:07 PM ()Reply
Hide this comment

Thanks, Gary. This did the trick. (I appended "< int >" to the end and it worked like a charm.)

By on 6/23/2009 9:39 AM ()Reply
Hide this comment

There are several differences between an F# list and a typical IList. For one, they have different performance properties: an F# list is actually a linked list, meaning that e.g. accessing the i'th element of the list takes O(n) time. An IList is typically implemented using an underlying array, which means that access is O(1). On the other hand appending to the front of an F# list is O(1).Maybe more importantly, F# lists are immutable: e.g. you can use them in multithreaded code without locking.So anyway, since these differences are important, it makes sense to make conversions explicit.

By on 6/23/2009 3:01 AM ()Reply
Hide this comment

A follow-up question: Is there a way to define a function/operator, that would work like [/[| but would return ILists (of the appropriate type)? It would make my code look much cleaner. Thanks.

By on 6/23/2009 2:21 PM ()Reply
Hide this comment

No, there's not.

I suggest you to declare a function doing this conversion:

let ilist (x: _ array) = x :> System.Collections.Generic.IList<_>

let a = ilist [|1..10|]

It looks clean to me. If that's still too verbose, you might want to have an infix operator for this:

let (!!) = ilist

let b = !![|1; 2; 3|]

Laurent.

By on 6/23/2009 2:38 PM ()Reply
Hide this comment

Thanks, Laurent. I ended up using your ilist suggestion, but had to append "<_>" at the end to get it to work.

By on 6/24/2009 3:12 PM ()Reply
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper

Logging in...