public class MatchCollection : ICollection, IEnumerable

For some reason, it doesn't implement a generic IEnumerable, so it's not an 'a seq.

Try Seq.cast.

>Seq.cast;;

val it : (Collections.IEnumerable -> seq<'a>) = <fun:clo@0>

By on 3/2/2009 6:49 PM ()

For some reason, it doesn't implement a generic IEnumerable<fun:clo @0="">

The reason most likely being the fact that generics in .NET were non-existent at the time of writing. All (many, at least) collections originating from .NET 1 have the same 'problem'.

By on 3/2/2009 8:56 PM ()

So it's a breaking change to add 'T IEnumerable to the IEnumerable interfaces?

By on 3/2/2009 9:03 PM ()

Hi,
for some (unknown and unfortunate!) reason, the MatchCollection doesn't implement the generic version of the IEnumerable interface. It implements only IEnumerable, but not IEnumerable<T> (which corresponds to seq<'a> in F#). You can use the Seq.cast function to convert the "untyped" version to a standard (typed) sequence like this:

1
2
3
 
let r = new Regex(...)
r.Matches("abcd") |> Seq.cast |> Seq.iter (fun (m:Match) -> printfn "%A" m);;

Note that you'll need the type annotation "m:Match", because it specifies the target type. Otherwise the F# compiler will not know what's the right type to use (and it wouldn't let you access the properties such as m.Value, m.Length etc.)

By on 3/2/2009 6:48 PM ()

Got it. I hadn't realized that distinction.

Is there something in this output ...

> (new Regex ".*").Matches("foo")
;;
val it : MatchCollection = seq [foo {Captures = seq [...]; etc

... that could have pointed me in the right direction? Or do I just need to get in the habit of noticing when collection-type things are generic in the doc?

By on 3/2/2009 7:59 PM ()

No, I don't think so. Sometimes it'll print out seq < type >, but I'm not sure of the rules on that. For example:

> new System.Collections.Generic.List< int >([1;2;]);;

val it : Collections.Generic.List< int > = seq [1; 2]

No indicator there that List is seq < int >.

By on 3/2/2009 8:55 PM ()
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