Demystifying F#'s Unit Type: The One-Value Wonder
Introduction
Coming from a C# background as a developer, the first time I saw unit type in F#. In my interpretation, I was dealing with the void
keyword.
Moreover, it isn't a direct equivalent but the closest one when a function does not return any meaningful value (this is also true in other programming languages).
That's why, in this post, we'll try to explore a unit type in F# and give examples of it.
Okay, then, let's get started.
What's a Unit Type?
In F#, a "unit type" is a type that represents a value with only one possible instance, denoted as ()
.
In other words, unit type indicates the absence of a specific value but describes a set with only a single element denoted by ()
.
Usefulness of Unit Type
I think that for about a year of working with F# language, unit type has been helpful in F# to ensure a consistent and type-safe approach when dealing with functions that don't produce intended results or are just used for side effects.
Example
The example below is a console application.
But let's try to play around before looking at the console application.
(* I created a small function to check if the type is a unit. *)
let isUnitType (value: 'a) : bool =
match value with
| () -> true
| _ -> false
From the function above, it checks if the passed value is a unit.
let myUnit = ()
let result: bool = myUnit |> isUnitType
Console.WriteLine("Test myUnit using the func isUnitType: {0}", result)
Just a quick note: If we're going to execute the let myUnit = ()
run on the interactive; it will result into
.t myUnit = ();;
val myUnit: unit = ()
Okay, going back to the example, the last line Console.WriteLine("Test myUnit using the func isUnitType: {0}", result)
will result in: "Test myUnit using the func isUnitType: True."
Let's try another one.
let saySomething() :unit =
printfn "I love F#"
()
let result1: bool = saySomething() |> isUnitType
Console.WriteLine("Test saySomething using the func isUnitType: {0}", result1)
This will be just the same. I hope you'll try it on yourself.
Okay, now for the entire code sample, please see it below.
open System
(* I created a small function to check if the type is a unit. *)
let isUnitType (value: 'a) : bool =
match value with
| () -> true
| _ -> false
let myUnit = ()
let result: bool = myUnit |> isUnitType
Console.WriteLine("Test myUnit using the func isUnitType: {0}", result)
let saySomething() :unit =
printfn "I love F#"
()
let result1: bool = saySomething() |> isUnitType
Console.WriteLine("Test saySomething using the func isUnitType: {0}", result1)
let saySomethingAgain() :unit =
printfn "I love C#"
let result2 = saySomethingAgain() |> isUnitType
Console.WriteLine("Test saySomethingAgain using the func isUnitType: {0}", result2)
0
For the sample output, see the screenshot below.
![](https://www.jinoncode.dev/content/images/2023/07/fsharp_unit_type_screenshot.png)
Summary
In this post, we have explored the F#'s unit type and given our opinion about its usefulness.
We have also shown code examples for us to understand its use and behavior.
I hope you have enjoyed this article. Till next time, happy programming!