Here's what you need (F# 1.1.11.7).

1
2
3
4
5
6
7
8
9
10
11
12
 

type INamed = interface
    abstract Name : string with get
end

// All objects in the collection should implement the INamed Interface
type 'a typed_collection when 'a :> INamed = class
    val coll : List<'a>
    member x.get_Item(nm:string) = x.coll.Find(fun (y :> INamed) -> y.Name = nm) 
end

However, there are a number of minor glitches with type constraints on class type variables in the current release (we only added support for this in the last release - see the Change ): we're working on fixing them. If you hit them, you can workaround them by using an unconstrained type variable and using "box" instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
 

type INamed = interface
    abstract Name : string with get
end

// All objects in the collection should implement the INamed Interface
type 'a typed_collection = class
    val coll : List<'a>
   member x.get_Item(nm:string) = x.coll.Find(fun (box y :?> INamed) -> y.Name = nm) 

end
By on 6/23/2006 10:51 AM ()

Din't know what went wrong there... Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 

open Microsoft.FSharp.Idioms

open System
open System.Collections.Generic

type INamed = interface
    abstract Name : string with get
end

// All objects in the collection should implement the INamed Interface
type 'a typed_collection = class
    val coll : List<'a>
   member x.get_Item(nm:string) = x.coll.Find(fun y -> (box y :?> INamed).Name = nm)  

end

By on 6/28/2006 2:01 PM ()

Hi Don,

I am still getting a syntax error when I copied your code. I am using F# 1.1.11.7. Here is the code that I am looking at.

open Microsoft.FSharp.Idioms
type INamed = interface
abstract Name : string with get
end

// All objects in the collection should implement the INamed Interface
type 'a typed_collection = class
val coll : List<'a>
member x.get_Item(nm:string) = x.coll.Find(fun (box y :?>(syntax error) INamed) -> y.Name = nm)

endIs there a typo somewhere?ThanksChris

By on 6/26/2006 10:53 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