开发者

Are F# record types implemented as structs or classes?

I'm using record types in F# to store some simple data, e.g. as follows:

open Vector

type Point =
    {
        x: float;
        y: float;
        z: float;
    }
    static member (+) (p: Point, v: Vector) = { Point.x = p.x + v.x ; y = p.y + v.y ; z = p.z + v.z }
    static member (-) (p: Point, v: Vector) = { Point.x = p.x - v.x ; y = p.y - v.y ; z = p.z - v.z }
    static member (-) (p1: Point, p2: Point) = { Vector.x = p1.x - p2.x ; y = p1.y - p2.y ; z = p1.z - p2开发者_Python百科.z }
    member p.ToVector = { Vector.x = p.x ; y = p.y ; z = p.z }

I can't work out whether this will be implemented as a value or reference type.

I've tried putting [<Struct>] before the type definition but this causes all sorts of compile errors.


[<Struct>] is the correct syntax for requesting a value type. It can be seen used in Chapter 6 of 'Expert F#', and this is accepted by F# 2.0:

[<Struct>]
type Point =
  val x: float
  new(x) = {x=x;}

Though if you write it as [<Struct>] type Point = (ie all on one line) it produces a number of warnings (no errors, though). Which version of F# are you using?


Records are classes, but all fields are immutable by default. In order to use the "advantage" of reference types, you must set the fields as mutable (you can set some as immutable and some as mutable) and then modify their value:

type Point =
    {
        mutable x : float
        y : float
        z : float
    }
    member p.AddToX Δx = p.x <- p.x + Δx


According to this wikipedia article, http://en.wikipedia.org/wiki/F_Sharp_(programming_language), record types are implemented as classes with properties defined.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜