Part 3
Powerful .flatMap Function on Result + Swift's Type Inference
We continue implementing our custom JSON decoding function, but this time by leveraging the powerful .flatMap function applied on a Result type. We will learn how to replace the function that we wrote last time (which was returning an optional custom structure), with another function that will instead return a Result of our custom structure. Advantages? The Result return type is an enum, and that means it will either return the decoded custom structure, if everything went well, OR it will give the caller an Error object, and that object will contain information about the cause of the decoding failure. Much better this way, to get some Error information when decoding failed, comparing to getting nothing (aka nil) like last time.
This video also touches on Swift's compiler powerful ability to infer type for variables and constants, which reduces substantially the amount of code we need to write, and most of all, we need to read. Using type inference together with the .flatMap function on Result, we will reduce our new function to a simple line of code.
Outline
How the .flatMap function can be applied on the Result type.
Swift type inference, which allows us to write a lot less code and makes written code much easier to read and understand.