Structures

Introduction

A structure is a list of words that describe a small object and actions that can be performed on that small object. A structure itself is not a true object. It only provides words, as members, that would be used to create an object.

Starting a Structure

The basic formula to create a structure is:

type structure-name = . . .

The creation of a structure starts with the type keyword followed by a name and =.

The Body of a Structure

The body of a structure is the section that starts after the = symbol. If you are create a very small structure, you can complete the creation just after that = symbol.

An Empty Structure

The body of a structure can start with the struct keyword and end with the end keyword. As a result, a structor can be created as follows:

type structure-name = struct end

Notice that, in this case, there is nothing between the struct and the end keywords. Here is an example of creating such a structure:

type email = struct end

This type of structure is referred to as empty.

Populating a Structure

From the above updated formula, the section between the struct and the end keywords is the body of the structure. In that section, you can create the members you want. If your structure needs just one member, you can create it between the struct and the end keywords. Here is an example

type email = struct member this.recipient = string end

In the real world, most structures have more than one member. In this case, each member should be created on its own line. The formula to create a structure becomes:

type StructureName =
    struct
	members
    end

Creating a Structure

In reality, you always ought to indicate that you are creating a structure. As a matter of fact, the struct and the end keywords are an optional combination. This means that if you use struct, then you must also end the structure with the end keyword. An alternative is that, if you omit the struct and the end keywords, then you must start the structure with [<StructAttribute>]. The formula to use becomes:

[<StructAttribute>]
type structure-name =
    members

The body of the structure contains its members.

The Member Variables of a Structure

The member variables of a structure are used to carry the values of the structure. The formula to create a member variable is:

val member-name: data-type[;]

A member variable is created using the val keyword, a name, a colon, and the desired data type. The ending semicolon is optional. Here are examples:

type Course =
    struct
        val CourseCode : string
        val CourseName : string
        val Credits : int
    end

Creating an Object From a Structure

To create an object from a structor, declare a variable and assign an instance of the structure to it. To access a member of the structure outside the structure, apply a period to the variable followed by the name of the member. Remember to appropriately initialize the members of the structure. Here is an example:

type Course =
    struct
        val mutable CourseCode : string;
        val mutable CourseName : string;
        val mutable Credits : int;
    end

let mutable study = new Course()

study.CourseCode <- "CMSC 140"
study.CourseName <- "Introduction to F# Programming"
study.Credits <- 3

printfn "Course Description";
printfn "Course Code: %s" study.CourseCode
printfn "Course Name: %s" study.CourseName
printfn "Course Credits: %i" study.Credits

This would produce:

Course Description
Course Code: CMSC 140
Course Name: Introduction to F# Programming
Course Credits: 3
Press any key to continue . . .

The Constructors of a Structure

Introduction

Like a class, a structure can have a constructor, but the rules are different. After creating the members of a structure, if you want to be able to initialize an object using the name of the structure, you should add a constructor using the new keyword. Such a constructor should include all the regular members of the structure. You can then use that constructor to create an object that is initialized. Here is an example:

type Course =
    struct
        val CourseCode : string;
        val CourseName : string;
        val Credits : int;
        new(code : string, name : string, credit : int) = {
            CourseCode = code; CourseName = name; Credits = credit }
    end
    
let study = new Course("CMSC 140", "Introduction to F# Programming", 3);

printfn "Course Description";
printfn "Course Code: %s" study.CourseCode
printfn "Course Name: %s" study.CourseName
printfn "Course Credits: %i" study.Credits

Structures and the Primary Constructor

Instead of a new constructor, you can create a primary constructor on the name of the structure.  Here is an example:

type Course(code : string, name : string, credit : int) =
    struct
        val mutable CourseCode : string;
        val mutable CourseName : string;
        val mutable Credits : int;
    end

If you create a primary constructor, you must provide a default value for each member. To do this, each member must be marked with the [<DefaultValue>] attribute. Here are examples:

type Course(code : string, name : string, credit : int) =
    struct
        [<DefaultValue>]
        val mutable CourseCode : string;
        [<DefaultValue>]
        val mutable CourseName : string;
        [<DefaultValue>]
        val mutable Credits : int;
    end

Previous Copyright © 2014-2024, FunctionX Monday 14 February 2022 Next