Let’s say you’ve decided to start a lottery. It’s a very simple lottery:

You buy a ticket for $1 that tells you if you win or lose.

If you lose, your $1 goes into the prize pool.

If you win, you win the entirety of the prize pool.

Let’s begin coding. First, we set some variables that contain the initial value of the prize pool, the chance of winning, and the amount of games to be simulated.

Then we make a function that updates the prize pool and dispenses a ticket, which is a number if you won, or undefined if you lost.

And then let’s simulate some games and print some stats:

As we’d expect from a lottery with a 10% chance of winnning, we’re not doing so hot after spending $10000 on tickets:

Thankfully, the lottery has a program to help people who got in debt because of it. They hire us to make their code more “succinct and readable”.

You have no idea what that means, but they also suggest that you use Option<T> from the library @oofdere/crabrave. You oblige, because you have no idea what any of this has to do with succulents and maybe this will give you a clue.

First, you import the Option utilities:

Then, you update the play() function to return an Option<T> enum:

Now we can run simulations like so:

Or even more succinctly:

Options force you to handle the lack of a value

On the surface Option<T> just looks like a less efficient/more complicated version of T | undefined. The big difference is that with Option<T>, you must always handle the None case, wheras with undefined, you might not even know to expect it.