Functional Programming Option type is your best friend against null and null-related errors (Have you ever heard of NullPointerException?). Option rep

Functional Programming Option type – Introduction

submited by
Style Pass
2021-07-20 16:30:07

Functional Programming Option type is your best friend against null and null-related errors (Have you ever heard of NullPointerException?). Option represents some value, using Some, or the absence of it, using None.

In this article, I am going to introduce you to the Option type: what is it, why would you want to use it, and how it works. The examples are written in Dart using the fpdart package, Functional Programming in dart and Flutter.

When you assign a type to a variable, for example String, it’s because you want to help yourself by saying: “This value is a String, and, from now on, I can use it and access methods only where a String is accepted!”.

This contract makes your life as a developer much easier; you set a limit for yourself that the compiler will ensure you will not be able to cross.

But, guess what, null is the exception! null is the only value that is not a String but it is still allowed to enter places where only a String is allowed to enter.

Leave a Comment