File: Interfaces.fs

package info (click to toggle)
mono 6.12.0.199%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,192 kB
  • sloc: cs: 11,181,844; xml: 2,850,076; ansic: 689,413; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,845; makefile: 19,951; sh: 15,030; python: 4,771; pascal: 925; sql: 859; sed: 16; php: 1
file content (53 lines) | stat: -rw-r--r-- 1,635 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module Interfaces

// You can implement one or more interfaces in a class type by using the interface keyword, 
// the name of the interface, and the with keyword, followed by the interface member definitions, 
// as shown in the following code.
type IPrintable =
   abstract member Print : unit -> unit
   abstract member MyReadOnlyProperty :int

type SomeClass1(x: int, y: float) =
   interface IPrintable with
      member this.Print() = printfn "%d %f" x y
      member this.MyReadOnlyProperty = 10 

// To call the interface method when you have an object of type SomeClass, 
// you must upcast the object to the interface type, as shown in the following code.+
let x1 = new SomeClass1(1, 2.0)
(x1 :> IPrintable).Print()


// An alternative is to declare a method on the object that upcasts and calls the interface method, 
// as in the following example.
type SomeClass2(x: int, y: float) =
   member this.Print() = (this :> IPrintable).Print()
   interface IPrintable with
      member this.Print() = printfn "%d %f" x y
      member this.MyReadOnlyProperty = 10 

let x2 = new SomeClass2(1, 2.0)
x2.Print()

// Interface Inheritance
type Interface0 = interface
    abstract member Method1 : int -> int
end

// Interface Inheritance
type Interface1 =
    abstract member Method1 : int -> int

type Interface2 =
    abstract member Method2 : int -> int

type Interface3 =
    inherit Interface1
    inherit Interface2
    abstract member Method3 : int -> int

type MyClass() =
    interface Interface3 with
        member this.Method1(n) = 2 * n
        member this.Method2(n) = n + 100
        member this.Method3(n) = n / 10