Advertisement

Stylish F# pp 11-28 | Cite as

Designing Functions Using Types

  • Kit Eason
Chapter

Abstract

Object Oriented (OO) programming is currently the dominant design approach in almost all software development. In OO, the natural unit of work is the “object” or “class,” and design effort is focused on defining classes that have the right shapes, behaviors, and relationships for the tasks at hand. In F# the natural units of work are types, which describe the shape of data; and functions, units of code that take some input and produce some output in a predictable fashion. We start our journey into stylish F# coding by looking at how best to design and code relatively simple types and functions. We think about what type or types the function should take as parameters, and what type it should return. We code the body of the function, before circling back to the type signature to try and eliminate possible errors. We learn how to embed a type representing some business item in a module, together with supporting functions. We cover the importance of single-case Discriminated Unions and the usefulness of hiding the constructor. We see how to do pattern matching in the declaration of a function parameter, and to use operators to simplify common operations.

Copyright information

© Kit Eason 2018

Authors and Affiliations

  • Kit Eason
    • 1
  1. 1.Farnham, SurreyUK

Personalised recommendations