Overview

4 Designing function signatures and types

Functional programming in a statically typed language places special emphasis on the shape of function signatures, because the types of inputs and outputs are part of how code communicates intent. The chapter introduces arrow notation as a concise, readable way to describe function types, using the idea that a function takes one kind of value and produces another. It also shows how more complex signatures, such as higher-order functions, can still be read clearly with the same notation, making signatures easier to reason about than the corresponding C# delegate types.

Beyond readability, the chapter argues that signatures should be as informative as possible. Some signatures reveal very little, while others strongly suggest the function’s purpose, such as filtering or combining sequences. The text then turns to designing the data that feeds those functions, emphasizing that primitive types are often too permissive and can allow invalid states. Instead, custom types can encode domain rules directly, shifting validation into the type itself so that invalid values are ruled out earlier and functions become more precise, more robust, and easier to understand.

The chapter also introduces the idea of an honest function: one whose behavior is fully captured by its signature, without hidden exceptions, nulls, or other surprises. A signature is treated as a contract, so if a function can fail in ways not reflected by its type, it is considered dishonest. Finally, the chapter explains how to represent the absence of data with Unit rather than relying on void, since Unit works better with functional abstractions and makes it easier to treat side-effecting and value-returning functions uniformly. This helps avoid duplicated code and supports more flexible, composable APIs.

An honest function does exactly what the signature says: it maps all possible values of the input type(s) to a valid value of the output type. This makes the behavior of your functions predictable and your program more robust.
A dishonest function can have an outcome that isn’t accounted for in the signature.
An object or tuple can be viewed as a Cartesian product.

Summary

  • Make your function signatures as specific as possible. This makes them easier to consume and less error-prone.
  • Make your functions honest. An honest function always does what its signature says, and given an input of the expected type, it yields an output of the expected type—no Exceptions, no nulls.
  • Use custom types rather than ad hoc validation code to constrain the input values of a function.
  • Use Unit as an alternative to void when you need a more flexible representation for functions that return no data.

FAQ

What is the main goal of designing good function signatures in functional programming?Good function signatures make APIs clearer, safer, and easier to use. In FP, a signature should accurately describe the inputs a function accepts and the outputs it produces, helping prevent invalid states and making the function’s behavior easier to predict.
What does arrow notation mean for function signatures?Arrow notation is the standard FP way to write function types. For example, int → string means a function takes an int and returns a string. It is often more readable than C# delegate types like Func<int, string>.
How are multiple inputs written in arrow notation?Multiple inputs are grouped in parentheses, such as (int, int) → int. This reflects the idea that the function takes a tuple of inputs, even if it is written in C# as a method with multiple parameters.
Why are some function signatures more informative than others?Some signatures tell you a lot about what a function does. For example, (IEnumerable<T>, (T → bool)) → IEnumerable<T> strongly suggests filtering. A more informative signature helps callers understand the function’s purpose and expected behavior.
Why is dynamic a poor choice for function inputs?dynamic weakens compile-time safety. It allows invalid calls that only fail at runtime, such as passing a string where an age is expected. In FP and statically typed design, it is better to let the compiler rule out invalid inputs.
Why are primitive types often not specific enough for modeling domain values?Primitive types like int may allow values that are technically valid for the type but invalid for the domain. For example, an age should not be any integer; it should be constrained to a valid range. A custom type can express that constraint more precisely.
How does a custom type like Age improve function signatures?A custom type such as Age moves validation into the type itself, ensuring only valid values can be created. Then a function like CalculateRiskProfile(Age age) becomes more explicit and safer, because invalid ages cannot be passed in.
What does it mean for a function to be “honest”?An honest function does exactly what its signature promises. It returns a value of the declared type, does not throw exceptions, and never returns null. Its behavior can be predicted from its signature alone.
Why is void considered less ideal in functional programming?void is a special language construct for “no return value,” but it does not compose well with functional abstractions like Func. For example, a higher-order function that works with Func<T> cannot directly accept a void-returning method without special handling.
What is Unit, and why use it instead of void?Unit represents the absence of meaningful data as a type with exactly one value. It is useful when you want consistent handling of functions and actions, especially when adapting Action to Func<Unit>. This makes functional composition easier than using void.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free