F Sharp Programming/Classes

In the real world, an object is a "real" thing. A cat, person, computer, and a roll of duct tape are all "real" things in the tangible sense. When we think about these things, we can broadly describe them in terms of a number of attributes:


 * Properties: a person has a name, a cat has four legs, computers have a price tag, duct tape is sticky.
 * Behaviors: a person reads the newspaper, cats sleep all day, computers crunch numbers, duct tape attaches things to other things.
 * Types/group membership: an employee is a type of person, a cat is a pet, a Dell and Mac are types of computers, duct tape is part of the broader family of adhesives.

In the programming world, an "object" is, in the simplest of terms, a model of something in the real world. Object-oriented programming (OOP) exists because it allows programmers to model real-world entities and simulate their interactions in code. Just like their real-world counterparts, objects in computer programming have properties and behaviors, and can be classified according to their type.

While we can certainly create objects that represents cats, people, and adhesives, objects can also represent less concrete things, such as a bank account or a business rule.

Although the scope of OOP has expanded to include some advanced concepts such as design patterns and the large-scale architecture of applications, this page will keep things simple and limit the discussion of OOP to data modeling.

Defining an Object
Before an object is created, its properties and functions should be defined. You define properties and methods of an object in a class. There are actually two different syntaxes for defining a class: an implicit syntax and an explicit syntax.

Implicit Class Construction
Implicit class syntax is defined as follows:
 * Elements in brackets are optional, elements followed by a  may appear zero or more times.

This syntax above is not as daunting as it looks. Here's a simple class written in implicit style:

The code above defines a class called, which has three properties and two methods. Let's take a closer look at the following:

type Account( number : int, holder : string ) = class

The underlined code is called the class constructor. A constructor is a special kind of function used to initialize the fields in an object. In this case, our constructor defines two values,  and , which can be accessed anywhere in our class. You create an instance of  by using the   keyword and passing the appropriate parameters into the constructor as follows:

Additionally, let's look at the way a member is defined: member x. Deposit(value) = amount <- amount + value

The  above is an alias for the object currently in scope. Most OO languages provide an implicit  or   variable to access the object in scope, but F# requires programmers to create their own alias.

After we can create an instance of our, we can access its properties using   notation. Here's an example in FSI:

Example
Let's use this class in a real program:

The program has the following types:

The program outputs the following: Initializing account x.Number: 12345, x.Holder: Homer, x.Amount: 50 x.Number: 67890, x.Holder: Marge, x.Amount: 100

Transferring $30 from Marge to Homer x.Number: 12345, x.Holder: Homer, x.Amount: 80 x.Number: 67890, x.Holder: Marge, x.Amount: 70

Transferring $75 from Homer to Marge x.Number: 12345, x.Holder: Homer, x.Amount: 5 x.Number: 67890, x.Holder: Marge, x.Amount: 145

Example using the keyword
The  keyword is used for post-constructor initialization. For example, to create an object which represents a stock, it is necessary to pass in the stock symbol and initialize the rest of the properties in our constructor:

This program has the following types:

This program outputs the following (your outputs will vary): Symbol: "MSFT" (19.130000) Symbol: "NOC" (43.240000) Symbol: "YHOO" (12.340000) Symbol: "GM" (3.660000)


 * Note: It's possible to have any number of  statements in a class definition, although there's no particular reason to need more than one.

Explicit Class Definition
Classes written in explicit style follow this format:

Here's a class defined using the explicit syntax:

Each  defines a field in our object. Unlike other object-oriented languages, F# does not implicitly initialize fields in a class to any value. Instead, F# requires programmers to define a constructor and explicitly initialize each field in their object with a value.

We can perform some post-constructor processing using a  block as follows:

Notice that we have to add an alias after our constructor, ), to access the fields of our object being constructed. Each time we create a   object, the constructor will print to the console. We can demonstrate this using fsi:

Example Using Two Constructors
Since the constructor is defined explicitly, we have the option to provide more than one constructor.

This program has the following types:

Notice that our  fields are included in the public interface of our class definition.

This program outputs the following: Cars created c.used: false, c.owner: Steve, c.mileage: 0 c.used: true, c.owner: Bob, c.mileage: 83000

Steve drives all over the state c.used: false, c.owner: Steve, c.mileage: 780 c.used: true, c.owner: Bob, c.mileage: 83000

Bob commits odometer fraud c.used: false, c.owner: Steve, c.mileage: 780 c.used: true, c.owner: Bob, c.mileage: 0

Differences Between Implicit and Explicit Syntaxes
As you've probably guessed, the major difference between the two syntaxes is related to the constructor: the explicit syntax forces a programmer to provide explicit constructor(s), whereas the implicit syntax fuses the primary constructor with the class body. However, there are a few other subtle differences:


 * The explicit syntax does not allow programmers to declare  and   bindings.
 * Even though  fields can be used in the implicit syntax, they must have the attribute   and be mutable. It is more convenient to use   bindings in this case. Public   accessors can be added when they need to be public.
 * In the implicit syntax, the primary constructor parameters are visible throughout the whole class body. By using this feature, it is not necessary to write code that copies constructor parameters to instance members.
 * While both syntaxes support multiple constructors, when you declare additional constructors with the implicit syntax, they must call the primary constructor. In the explicit syntax all constructors are declared with new and there is no primary constructor that needs to be referenced from others.

In general, its up to the programmer to use the implicit or explicit syntax to define classes. However, the implicit syntax is used more often in practice as it tends to result in shorter, more readable class definitions.

Class Inference
F#'s  syntax allows programmers to omit the   and   keywords in class definitions, a feature commonly referred to as class inference or type kind inference. For example, there is no difference between the following class definitions:

Both classes compile down to the same bytecode, but the code using class inference allows us to omit a few unnecessary keywords.

Class inference and class explicit styles are considered acceptable. At the very least, when writing F# libraries, don't define half of your classes using class inference and the other half using class explicit style—pick one style and use it consistently for all of your classes throughout your project.

Instance and Static Members
There are two types of members you can add to an object:
 * Instance members, which can only be called from an object instance created using the  keyword.
 * Static members, which are not associated with any object instance.

The following class has a static method and an instance method:

We invoke a static method using the form. We invoke instance methods by creating an instance of our class and calling the methods using. Here is a demonstration in fsi:

We can, of course, invoke instance methods from objects passed into static methods, for example, let's say we add a  method to our object defined above:

We can experiment with this method in fsi:

is a static method on the  class which determines whether two objects instances are the same object. As shown above, our  method takes an instance of   and accesses its   property.

When should you use static methods rather than instance methods?

When the designers of the .NET framework were designing the  class, they had to decide where the   method should go. They had the option of making the property an instance method or making it static. The .NET designers chose to make  an instance method because it is an intrinsic property of strings.

On the other hand, the  class also has several static methods, including   which takes a list of string and concatenates them all together. Concatenating strings is instance-agnostic, its does not depend on the instance members of any particular strings.

The following general principles apply to all OO languages:


 * Instance members should be used to access properties intrinsic to an object, such as.
 * Instance methods should be used when they depend on state of a particular object instance, such as.
 * Instance methods should be used when its expected that programmers will want to override the method in a derived class.
 * Static methods should not depend on a particular instance of an object, such as.
 * Static methods should return the same value as long as the inputs remain the same.
 * Constants, which are values that don't change for any class instance, should be declared as a static members, such as.

Getters and Setters
Getters and setters are special functions which allow programmers to read and write to members using a convenient syntax. Getters and setters are written using this format:

Here's a simple example using fsi:

Getters and setters are used to expose private members to outside world. For example, our  property allows users to read/write to the internal   variable. Since getters and setters are glorified functions, we can use them to sanitize input before writing the values to our internal variables. For example, we can modify our  class to constrain our to values between 0 and 10 by modifying our class as follows:

We can use this class in fsi:

Adding Members to Records and Unions
Its just as easy to add members to records and union types as well.

Record example:

Union example

Generic classes
Classes which take generic types can be created:

We can use this class in FSI as follows:

Generic classes help programmers generalize classes to operate on multiple different types. They are used in fundamentally the same way as all other generic types already seen in F#, such as Lists, Sets, Maps, and union types.

Pattern Matching Objects
While it's not possible to match objects based on their structure in quite the same way that we do for lists and union types, F# allows programmers to match on types using the syntax:

Here's an example which uses type-testing:

This program outputs: Meow Hi, I'm Bob Hi, I'm Bill I don't recognize type 'String' I don't recognize type 'Monkey'