Hi,
you can do this by specifying how the basic numeric operations for your type should behave. This can be done by implementing an interface (INumeric<'a>) and by registering the implementation. The following code shows how to do this for a very basic record type:

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
 
#light

open Microsoft.FSharp.Math

type N = { Numb : float }

// Implement basic operations for our new type
let numN = 
  { new INumeric<N> with
      member x.Abs a = a
      member x.Add({ Numb = a }, { Numb = b }) = { Numb = a + b }
      member x.Multiply({ Numb = a }, { Numb = b }) = { Numb = a * b }
      member x.Negate({ Numb = a }) = { Numb = -a }
      member x.Subtract({ Numb = a }, { Numb = b }) = { Numb = a - b }
      member x.Parse(_, _, _) = failwith "not supported"
      member x.Sign({ Numb = a }) = sign a
      member x.ToString (n, _, _) = (box n).ToString()
      member x.One = { Numb = 1.0 }
      member x.Zero = { Numb = 0.0 }
   }

// Register the association
Math.GlobalAssociations.RegisterNumericAssociation(numN)

let vec = Vector.Generic.of_list [ { Numb = 1.0 }; { Numb = 2.0} ]
printfn "%A" (vec .* vec) // works!
By on 11/10/2008 3:47 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper