File: OperatorsOverloading.fs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (42 lines) | stat: -rw-r--r-- 1,447 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
module OperatorsOverloading

// The following code illustrates a vector class that has just two operators, one for unary minus 
// and one for multiplication by a scalar.  In the example, two overloads for scalar multiplication are needed 
// because the operator must work regardless of the order in which the vector and scalar appear.+
type Vector(x: float, y : float) =
   member this.x = x
   member this.y = y
   static member (~-) (v : Vector) =
     Vector(-1.0 * v.x, -1.0 * v.y)
   static member (*) (v : Vector, a) =
     Vector(a * v.x, a * v.y)
   static member (^^^) (a, v: Vector) =
     Vector(a * v.x, a * v.y)
   static member (?<-) (a, v, b: Vector) =
     Vector(a * b.x, a * b.y)
   static member (|+-+) (a : int, v: Vector) =
     Vector(0.0, 0.0)
   static member ( ~~~ ) (v: Vector) =
     Vector(0.0, 0.0)
   static member ( + ) (v: Vector, v2: Vector) =
     Vector(0.0, 0.0)
   static member ( .. .. ) (start, step, finish) =
     Vector(0.0, 0.0)
   static member ( .. ) (start, finish) =
     Vector(0.0, 0.0)
   override this.ToString() =
     this.x.ToString() + " " + this.y.ToString()

let v1 = Vector(1.0, 2.0)
let v2 = v1 * 2.0
let v4 = - v2
let v5 = 1 |+-+ v2
let v7 = ~~~ v4

printfn "%s" (v1.ToString())
printfn "%s" (v2.ToString())
printfn "%s" (v4.ToString())

let v9 : ('T2 -> 'T3) -> ('T1 -> 'T2) -> ('T1 -> 'T3) = Operators.(<<)
let v10 : ('T2 -> 'T3) -> ('T1 -> 'T2) -> ('T1 -> 'T3) = Operators.(<<)