Since Cyber Security Concepts and Education, I've been working through various projects and recently I found myself working with the Microsoft Windows API.

Anyone who's ever worked with C/C++ knows that a traditional way of getting data from any function is to inspect its return value. If you want to get multiple values back, a useful but clunky way is to pass out values back through 'out' parameters you initially supplied with the function call.

You might want to do this if you'd like to return, say an integer from the function, perhaps to indicate if the function was a success or not, and if not, you can inspect the reason set in one of the function's output parameters. This is usually done as pointers such that the function internals will modify the contents of the pointers and then you can inspect the return result and the pointers after you call the function. Something like this:

HRESULT result = WinApiFunc(int a, int b, char* buffer, int* count);

That might for example be used to have Windows write something into a buffer for you and write into a pointer how many/count bytes were actually written. So its returned multiple values but not all in the result result.

In LanguageExt you have the concept of an Either<X,Y> meaning you can get either and X or Y back from the function. This is useful because now you can use a wrapper function (that still calls this) but now deals with the 'out' parameters and resolves the situation to an Either return type: either it succeeded or if returned an error. 

I put together an Either type to achieve this in C++:

Either<int, string> result = windowsApiFunctionWrapper();

Why I find this particularly useful is because I can wrap Windows API calls with all their 'out' parameters mess into less cognitive wrapper functions that hide this mess and then return either an expected result or the problem details if they occurred within the wrapper function.

This means that wrapper functions just return a single return type of Either. This makes my code look less complex, saving me from having to litter my logic with checking the out parameters and return results while structuring my code to be simple (without so many conditional statements).

I've also put together an Option<T> type, a-la-Language-Ext also. These can be found here.

Its one small step to making working with the Windows API a little nicer to look at.