With type extension, records are like classes without inheritance, interfacing (I think), creation through constructors (as in let foo = new record_t()), and private members.

in the end it depends on what you need to do... you can combine both depending on your needs. For instance, a convoluted example could be :

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
 

module Point = //plain Point data structures that ned not be inherited / interfaced...
  type xy = {x:float; y:float} 
    with
      static member creat x y = {x=x ; y=y}
    end
  type bar = {o:float; h:float;l:float; c:float}   //ohlc -> open, high, low, close
    with
      static member create o h l c = 
        let max_, min_ = if o > c then o, c else , o
        if _max >= h || _min <= l then failwith "pb with input data : high is not the high or low is not the low"
        {o=o; h=h; l=l; c=c}
    end


type Points = XY of Point.xy array | Bar of bar list 


type Line(p:Points) =
  member x.Points = p
  member x.Min = 
    match x.Points with 
    | XY arr -> Array.fold_left (fun acc elt -> min acc.y elt.y) arr.(0) arr
    | Bar l -> List.fold_left (fun acc elt -> min acc.h elt.h) arr.(0) arr
//etc.
By on 9/26/2007 9:53 AM ()

Thanks for your reply.

It seems class is more clear and well-organised.

By on 9/27/2007 6:20 AM ()

Hi John,

Records are often used as implementation types where the actual record itself is hidden by a signature file and only the members are revealed. This is partly because you get a little bit more machinery for free with records, e.g. structural comparison (IComparable) and structural hashing (IStructuralHash) are implemented automatically for record types. But for many types this isn't particularly interesting or helpful.

There is nothing deep in this: conceptually records and classes are closely related and share most syntactic constructs. From the design and organization perspective of a public API the choice is largely moot as you should reveal nearly all functionality via members in both cases.

As I've mentioned elsewhere on thiese forums we may make a few non-breaking langauge extensions to bring records and object types into even closer syntactic alignment.

Cheers! :-)

Don

By on 9/27/2007 11:11 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