F# : static let and static member
Asked Answered
R

3

8

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...

Ragen answered 24/12, 2015 at 5:34 Comment(0)
B
8

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.

Beautify answered 24/12, 2015 at 5:38 Comment(1)
i should have read more carefully the book. it wasn't very well documented, but there was an example well hidden a few pages before.Ragen
G
1

static members belong to the type on which they are declared and are accessed by the fully qualified type and member name. In this instance

let zero = Vector2D.Zero
let oneX = Vector2D.OneX
let oneY = Vector2D.OneY

The Vector2D type does not currently have any instance members, but if it did, these would be accessible on an instance of Vector2D

Gap answered 24/12, 2015 at 5:46 Comment(0)
B
0

Paraphrasing the F# language guide's let Bindings in Classes article:

A let binding in a class creates a private field or function; to expose data or functions publicly, declare a property or a member method.

  • An instance let binding is a let binding that is not preceded by the static keyword.

    Instance let bindings execute when objects are created.

  • Static let bindings are part of the static initializer for the class.

    Static let bindings are guaranteed to execute before the type is first used.

If one tried to refactor the Vector2D type by removing the static keywords before zero, onex, and oney private functions,

type Vector2D(dx : float, dy : float) =
     let zero = Vector2D(0.0, 0.0)
     let onex = Vector2D(1.0, 0.0)
     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

then they would have received

error FS0039: The value or constructor 'zero' is not defined.

as zero and co. are only guaranteed to evaluate when the class is instantiated.

Bronchial answered 11/1 at 1:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.