1 people like it.

Complex 2 - First Operator

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
// Define complex type with a '+' operator
type Complex =
    { Re : float;
      Im : float }
    static member (+) (z1, z2) = 
        { Re = z1.Re + z2.Re; 
          Im = z1.Im + z2.Im };;

// .. and printing
let print z = printfn "%.3f%+.3fi" z.Re z.Im;;

// Test it
let z1 = {Re = 1.0; Im = 4.0};;
let z2 = {Re = 2.0; Im = -2.0};;

let z3 = z1 + z2;;

print z3;;
Complex.Re: float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

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

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
Complex.Im: float
val z1 : Complex
val z2 : Complex
val print : z:Complex -> unit

Full name: Script.print
val z : Complex
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val z1 : Complex

Full name: Script.z1
val z2 : Complex

Full name: Script.z2
val z3 : Complex

Full name: Script.z3
Raw view Test code New version

More information

Link:http://fssnip.net/6K
Posted:14 years ago
Author:
Tags: