Chapter 8. Classes
WHAT'S IN THIS CHAPTER?
Defining classes
Creating fields and constructors
Declaring members
Using access modifiers
Using type extensions
For the experienced C# or Visual Basic developer, this chapter will likely be the least conceptually new material found thus far in the book — as the F# reference states clearly, object-oriented programming is the dominant paradigm of the .NET ecosystem, and classes form the core means by which .NET developers organize their code. As a result, classes and objects are (or at least, should be) familiar ground, and F# supports the full range of object-oriented facilities offered by other languages. Although the F# syntax frequently expresses the same concept in a more terse fashion, overall the developer experienced with objects in C# or Visual Basic will find classes in F# to be a comfortable transition.
A larger danger lurks — as a fusion of both objects and functions, F# offers more than "just" another syntax for building object-oriented applications. As the danger of C++ was in using it as "a better C" and not seeing its deeper capabilities, the danger to the budding F# developer is in using it as "a better C#" and not seeing beyond the object facilities, thus ignoring the potential power of the synthesis of object and function.
BASICS
To create a new, empty, class, F# uses (again) the type keyword, and class/end markers to indicate the beginning and end of the class declaration:
type Example =
class
endThis introduces a new class into ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access