Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a new instance of a Structure in F#?

Tags:

f#

I feel really dumb about this one, but I am having a real hard time finding documentation on this.

If I declare a struct like so:

type BuildNumber = 
    struct
        val major : int
        val minor : int
        val build : int
        val revision : int
    end

Then how do I make a new instance of the BuildNumber type?

like image 936
Eric Dand Avatar asked Apr 15 '15 22:04

Eric Dand


People also ask

How do I create a new instance of a struct?

We create an instance by stating the name of the struct and then add curly brackets containing key: value pairs, where the keys are the names of the fields and the values are the data we want to store in those fields.

Which is the correct way to create an instance of struct type?

Struct Instantiation Using new Keyword An instance of a struct can also be created with the new keyword. It is then possible to assign data values to the data fields using dot notation.

Do structs have instances?

Structure types have value semantics. That is, a variable of a structure type contains an instance of the type. By default, variable values are copied on assignment, passing an argument to a method, and returning a method result. For structure-type variables, an instance of the type is copied.

How do you create a struct of an object?

Using the New Keyword to Create Objects Another way to create an object from a struct is to use a new keyword. While using a new keyword, Golang creates a new object of the type struct and returns its memory location back to the variable.


1 Answers

You use the new keyword and define a constructor for it.

For example:

type simple = 
    struct
        val A : int
        val B : int
        new (a: int, b: int) = { A = a; B = b; }
    end

let s = new simple(1, 2)
like image 167
DaveShaw Avatar answered Nov 06 '22 17:11

DaveShaw