Created
April 22, 2012 22:34
-
-
Save zeux/2467344 to your computer and use it in GitHub Desktop.
F# generic enumeration helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// helpers to iterate IDiaEnum* objects | |
let inline toSeq (o: ^T) = | |
let e = { new IEnumerable with member this.GetEnumerator () = (^T: (member GetEnumerator: unit -> _) (o)) } | |
if true then | |
Seq.cast e | |
else | |
// Dummy expression to constrain return type to that of o.Item | |
seq [| (^T: (member Item: _ -> _) (o, Unchecked.defaultof<_>)) |] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In FsControl there is a more generic version, which use a default-case which is equivalent to your code. If you extract that overload to a single function you get this:
Although it's better to use the even more generic
toSeq
function defined there.