I'm a noob in F# and currently reading Expert in F# 3.0. Its the first compiled language I'm learning (I just know to program in R)
on chapter 6, p.117, we are introduced without much ceremony static let and static member. i don't really understand what it is
type Vector2D(dx : float, dy : float) =
static let zero = Vector2D(0.0, 0.0)
static let onex = Vector2D(1.0, 0.0)
static let oney = Vector2D(0.0, 1.0)
/// Get the zero vector
static member Zero = zero
/// Get a constant vector along the X axis of length one
static member OneX = onex
/// Get a constant vector along the Y axis of length one
static member OneY = oney
There is no example who to proceed further in the book.
I'm typing this in F# interactive. From there, how do I construct a variable x whose value is zero (or onex...)?
I'm trying the following. Nothing works
let x = Zero;;
let y = Vector2D(2.0,2.0);; /// ok
y.Zero;;
stdin(237,1): error FS0809: Property 'Zero' is static
in http://fsharpforfunandprofit.com/posts/classes/ there is this example,
type StaticExample() =
member this.InstanceValue = 1
static member StaticValue = 2 // no "this"
// test
let instance = new StaticExample()
printf "%i" instance.InstanceValue
printf "%i" StaticExample.StaticValue
so i would have expected y.Zero;;
to yield something above ?...
thanks. sorry the question is so basic. If someone can explain me what it is about...
So the basic difference is that static members don't belong to an instance of a class.
rather than y.Zero
, you get Vector2D.Zero
.
To a first approximation you can think of these examples of properties of the type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With