5 people like it.

Simple Object orientation using Record types

Simple Object orientation using Record types. No complex - private public internal virtual abstract object oriented programming :)

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
type Adder = 
    {
        add : unit -> int;
        getX : unit -> int;
        getY : unit -> int;
        setX : int -> unit;
        setY : int -> unit;
    }

let goodAdder x y = 
    let _x = ref x
    let _y = ref y
    let add () = !_x + !_y
    let getX () = !_x
    let getY () = !_y
    let setX x = _x := x
    let setY y = _y := y
    { add = add; getX = getX; getY = getY; setX = setX; setY = setY}

let weirdAdder x y = 
    let baseAdder = goodAdder x y
    let weirdAdd () = baseAdder.add() + 10
    { add = weirdAdd; getX = baseAdder.getX; getY = baseAdder.getY; 
    setX = baseAdder.setX; setY = baseAdder.setY}
Adder.add: unit -> int
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
Adder.getX: unit -> int
Adder.getY: unit -> int
Adder.setX: int -> unit
Adder.setY: int -> unit
val goodAdder : x:int -> y:int -> Adder

Full name: Script.goodAdder
val x : int
val y : int
Multiple items
val ref : value:'T -> 'T ref

Full name: Microsoft.FSharp.Core.Operators.ref

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
val add : (unit -> int)
val _x : int ref
val _y : int ref
val getX : (unit -> int)
val getY : (unit -> int)
val setX : (int -> unit)
val setY : (int -> unit)
val weirdAdder : x:int -> y:int -> Adder

Full name: Script.weirdAdder
val baseAdder : Adder
val weirdAdd : (unit -> int)
Raw view New version

More information

Link:http://fssnip.net/2K
Posted:6 years ago
Author:Ankur Dhama
Tags: records , objects