Union GeneratorWhat inspired us?We've been really happy using freezed package tool to create our union classes, which have given us the possibility of having a better solution than common enums, providing methods like map, when, maybeMap and maybeWhen that allowed us to avoid omissions when working with different possible enumerated values for an instance. So, what's the problem? Well... Freezed package creates auto-generated code and this, of course, makes stuff to be impossible to control, while adds some extra dependencies that we wanted to avoid at all. Now, we're going to explain how to use the extension, so you can have full control on your code while easily having all the advantajes that unions give us. UsageThis extension works by creating a template class that will help us know what to generate next. The expected structure is as follows:
This example will generate a type YourClassName which can be of any of these subtypes: NameForFirstUseCaseClass || NameForSecondUseCaseClass || NameForThirdUseCaseClass Also, this will provide you the handy methods map, when, maybeMap and maybeWhen. To activate generation you must place the cursor in the class declaration line, and press cntrl (cmd) + . Why would I use Unions?This is a perfect solution since you don't need to remember every single possible value that an instance can have, as the provided methods will handle this task themselves. What to do if you can't generate the UnionsHere we'll describe some cases where you could have some troubles trying to use the extension. First of all, as it's on a very early stage, some cases are not (yet) supported:
SupportWe highly encourage you to create a pull request for this extension if you want to improve it. Your help is always welcome. Also, you can open issues if there's any trouble. |