Home

Structures

 

Structures

 

Introduction

A structure is an architectural means of describing small objects. A structure itself is not a true object. It only provides words, as members, that would be used to create an object.

 

Creating a Structure

The basic formula to create a structure is:

type StructureName =
    struct
	Members
    end

The creation of a structure starts with the type keyword followed by a name and =. The body of a structure starts with the struct keyword and ends with the end keyword. 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 StructureName =
    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 MemberName: DataType[;]

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 structor 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, chaque 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-2015 FunctionX Next