Just trying to declare an array object. Easy?
Array of basic type
Declare an array of
Int objects with explicit type:
You can add the object initialization:
But in Swift you can use the type inference and be much shorter:
Here come the inner class
Let say we have a class that define an inner enumeration type:
Can we define an array of such an object?
That seems to be working. No error, no warning, the object is properly created in a playground.
Can we reproduce the pattern that we used with
Int and define an array of
This uses the type inference and call the array initialization. What can go wrong in this syntax?
Well, try this and you will get the following error:
That seems quite strange no?
Let’s try with another syntax. Keep the déclaration and use empty array syntax to define the value, like this:
This time the syntax is valid, the object is properly defined. No more syntax error.
But could we go back to the syntax we used with array if integer? Yes we can. But you need to wrap the inner enumeration inside a type alias, like the following example:
Here we are. A working array definition with the type specified in the definition.
Can someone explain me why we need to use a type alias?
Is this tied to internal implementation of the alias, or is this some kind of bug in the way the compiler interprets the syntax of inner type?