Now one common source of confusion.
I want to clarify here is the difference between generics and union types.
Why could you confuse these two.
Well for example our data storage here.
If we're accepting generic types based on these types we could all rewrite that to just say well we
want to store strings or numbers or billions in here.
Right.
So we could store that or to be precise.
Wrap this in parentheses so that we say either of these types as an array.
And then here of course we could also say well what we get here when we add item is either a string
a number or boolean we remove it.
It's the same.
Now what's the problem with this approach.
On first sight it might look like it achieves the same but actually does something totally different.
What we're saying here is we're storing any kind of data in there as long as it's ever in the array
of strings numbers are billions.
And we're also then adding any kind of data here even a string a number or boolean and the same for
removing and we already got some errors down there of course because it can't initialized isn't a generic
way anymore.
But other than that it works.
Now the problem however is for one up here we're not saying this is ever an array of strings or in arrays
of numbers or an array of ebullience.
This says well we got an array which can have strings numbers and boolean is mixed.
So if we would want to say either an array of strings or an array of numbers we would have to say string
array number array or boolean array.
Well we can do that but now we get a problem here.
Now we're adding a string a number or a boolean but depending on what we actually set this data array
to be either a number array or a billionaire rate where a string array we're not allowed to add a number
or a boolean or a string.
If we set this to be a String array well then we can't add a number here yet here I am fine with any
parameter as long as it is a number boolean or string.
So the problem is here with union types we accept any of these values every time this method gets called
or this method.
So we're not saying hey whenever we're working with this class you have to decide for one type and then
stick to it.
Instead we're saying whenever you're calling this method you're free to use any of these types.
And that's exactly not what we want here.
If a reverts this to the previous setup where we used generic types
then we got a different setup here we say you got to choose ones which kind of data you want to store
and then you're only allowed to add that exact type of data so yea if I add a number for example I get
an error because that's not a string and I chose that I only want to manage strings here.
When I set the generic type and that's the difference between union types and generic types union types
can be great.
If you want to have a function which you can call with one of these types every time you call it generic
types are great.
If you want to lock in a certain type you is the same type throughout the entire class instance you
create you use the same type throughout the entire function that's where you want the generic type you
want union types when you are flexible to have a different type with every method call with every function
call then you can use union types generic types lock in a type.