It is possible to define delegates but they are rarely useful within pure F# code as you can treat any function as a value. They can be usefull for interop. Here is how you define one:

1
2
open System
type ItemHandler = delegate of obj * EventArgs -> unit

Here is an apporimation of your interface. I can not find a way to constrain type parameters on types, I'm not sure if this is implmented yet.

1
2
3
4
5
6
7
8
9
10
open System
open Idioms

type ('T, 'U) ITestInterface =  interface 
    abstract ItemReceived : IEvent<System.EventArgs>
    abstract DataObject : 'U  
        with get
    abstract Data : 'T
        with get
end

It is possible to constrain types on values (function parameters):

1
2
let do_comparable_thing (thing1 : 'T) (thing2 : 'U when 'U :> IComparable<'T>) =
    thing2.CompareTo(thing1)

It is not possible to create private members yet, you can make the whole type private via an interface file (.fsi file), but you can hide just parts of it. This has been promised for a future release.

By on 6/14/2006 1:59 AM ()

Hi,

F# programming typically uses constrained generics, classes and interfaces in a fairly different way to C# programming.

For example, the use of IComparable and new() constraints in F# code is rare. Constraints themselves are used, but more rarely and in a different sort of way - F# programmers often choose to model relationships between types by explicit functions that map between types, rather than implicitly through subtyping and constraints. Thus it is more common to simply pass comparer functions (logically equivalent to comparer delegate values) and generator function values explicitly. For example, the functionality implicit in your constraints may be made explicit in the arguments to a constructor function that closes over these values. This means types often have "thinner" core representations (e.g. they are just a record or, if subtyping is required, an interface), and classes become constructor functions.

1
2
3
4
5
6
7
8
type TestType<'T,'U> = { Data: 'T; DataObject: 'U; }

let mkTest (mkT : unit - > 'T) 
           (mkU : unit -> 'U)
           (compareT : 'T -> 'T -> bool) = 
   { new TestType<'T,'U> 
     with Data = ... // may use mkT, mkU and compareT here
     and DataObject = ... }

Multiple function values are often packaged into records or interfaces, but classes are used much less often,since interfaces can often be implemented by object expressions.

I'd also recommend you start by learning functional programming (e.g. from Jason Hickey's OCaml book).

Cheers!
Do

By on 6/14/2006 6:02 AM ()
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