loading...
Cover image for What's the Unit Type in F#?

What's the Unit Type in F#?

josegonz321 profile image Jose Gonzalez ・2 min read

Originally posted at: http://www.mindbodysouldeveloper.com/2017/04/08/whats-the-unit-type-in-f-sharp/

My F# adventure continues.

I quickly learned that in functional programming, every function must return a value.

For example:

let Area length height =
    length * height

The Area function above will always return whatever the value is of length * height.

What if you don't want your function to return anything? 🤔

Let's say you have the following C# code

void PrintNumbers(int min, int max)
{
    for (int i = min; i < max; i++)
        Console.WriteLine(i);
}

Same code in F#:

let PrintNumbers(min max)
    for x in min..max do
        printfn "%i %i" x

(Side note: I love F#'s simplicity and elegance).

Our PrintNumbers function didn't return anything. It just printed the numbers on the screen.

What the hell? I thought every function must return something in functional programming or F#.

You might be thinking...and you are correct!

Have a cookie. 🍪

Oh, but I can't give it to you via the computer screen. Oh well, I will eat it for you! (YUM, YUM)

What's an unit type?

Hold up. Let me clean up some of these cookie crumbles before I continue.

Alright, cool. So, if you place your cursor over the PrintNumbers function. Notice the tooltip:

Unit Type

It says, this function takes two integers and returns a unit value or nothing.

In other words, unit is equivalent to void in C#.

Now for the official (and dry definition) 🙄.

MSDN says:

The unit type is a type that indicates the absence of a specific value; the unit type has only a single value, which acts as a placeholder when no other value exists or is needed.

TL;DR

This has been a super short introduction to the unit type. I'm sure there is more to it than what I've explain on this post.

Hopefully, you won't be weirded out when you see unit in your F# adventure.

Again:

F# unit = C# void

What's your experience with F# unit? What else can you share with us? Let us know on the comments below!

Until next time, take care.

P.S. The cookie I ate for you was freaking delicious. 😊

Special thanks to Kit Eason for inspiring this post. Check out his awesome F# Jumpstart course at Pluralsight.

Discussion

pic
Editor guide
Collapse
eljayadobe profile image
Eljay-Adobe

F# as an FP language is... wow! And The Book of F# by Dave Fancher is a great introduction and tutorial for the language. In my opinion, that book is far better than the other dozen F# books I've read.

What do you think of using F# as an OO language? A "better C# than C#" kind of language?

I know that F# positions itself as a FP language, but since Don Syme had to hammer the .NET requirements on top of OCaml, F# is also a top-shelf OO language like C# or VB.NET.

Collapse
josegonz321 profile image
Jose Gonzalez Author

Hey Eljay,

I have not read The Book of F#. But I have watch Dave's pluralsight courses and his YouTube videos.

I met Dave at KCDC this past summer. What an outstanding guy. Friendly and super down to earth.

F# is flexible enough to be used as an OO language. I don't think it should be used that way. For me, it's not a dilemma between F# and C#, it's between functional programming and imperative programming.

C# can do functional programming but it's such a pain!
F# can do imperative programming but it's ugh.

Since I'm lit up on functional programming, I lean towards FP languages.