Member-only story

Functional Programming In Flutter, Sandbox Your Functions

Fred Grott
4 min readJul 9, 2021

--

Dart, like Kotlin and Swift, is a mixed language with equal parts of OOP and FP. The first step in implementing and using Functional Programming is to sandbox some functions.

Pure VS Non-Pure Functions

Pure functions are called pure functions because they do not have any unintended effects; i.e. they do not touch things such as system services, etc. Your concern should be all those non-pure functions. As, since they can trigger unintended effects they should be sandboxed so, we get predictable behavior.

The Sandbox Pattern

We can use the type-alias(typedef) pattern to set up a callback type:

Since this is the first step we are not going to mix FP into the middle of these functions at this time. Later on, as you learn more FP, you will translate the try-catch blocks into Either-task constructs.

To use in a non-Future way, it would be:

guardVoided(myVoidedFunction);

--

--

Fred Grott
Fred Grott

Responses (1)