Using interfaces

Category:
Defining Types and Functions
Description: A longer sample showing how to use interfaces to model 'data' objects such as abstract points. Somewhat contrived, since multiple repreentations of points are unlikely practice, but for larger computational objects maintaining flexibility of representation through using interfaces or function values is often crucial.
Code:
type IPoint = 
abstract X : float
abstract Y : float


/// Implementing an interface with an object expression.
let Point(x,y) =
{ new IPoint with
member obj.X=x
member obj.Y=y }

/// Implementing an interface with an object expression that has mutable state
let MutablePoint(x,y) =
let currX = ref x
let currY = ref y
{ new IPoint with
member obj.X= currX.Value
member obj.Y= currY.Value }

/// This interface is really just a function since it only has one
/// member, but we give it a name here as an example. It represents
/// a function from some variable to (X,Y)
type ILine =
abstract Get : float -> IPoint

/// Implementing an interface with an object expression.
///
/// Here a line is specified by gradient/intercept
let Line(a:float,c:float) =
let y(x) = a * x + c
{ new ILine with
member l.Get(x) = Point(x, y(x)) }

/// Implementing an interface with a class.
///
/// Here a line is specified by gradient/intercept
type GradientInterceptLine(a:float,c:float) =
// Some local bindings
let y(x) = a * x + c

// Publish additional properties of the object
member x.Gradient = a
member x.Intercept = c

// Also implement the interface
interface ILine with
member l.Get(x) = Point (x,y(x))

let InterfaceSample3() =

// This creates an object using a function
let line1 = Line(1.0, 0.344)

// This creates a similar object using a type.
let line2 = new GradientInterceptLine(2.0,1.5) :> ILine
let origin = { new IPoint with
member p.X =0.0
member p.Y = 0.0 }
let point1 = line1.Get(-1.0)
let point2 = line2.Get(0.0)
let point3 = line2.Get(1.0)
let outputPoint os (p:IPoint) = fprintf os "(%f,%f)" p.X p.Y
printfn "origin = %a" outputPoint origin;
printfn "point1 = %a" outputPoint point1;
printfn "point2 = %a" outputPoint point2;
printfn "point3 = %a" outputPoint point3

Execution Result:
origin = (0.000000,0.000000)
point1 = (-1.000000,-0.656000)
point2 = (0.000000,1.500000)
point3 = (1.000000,3.500000)

Last edited Sep 14, 2011 at 2:30 AM by ttliu2000, version 1

Comments

No comments yet.