SLN SLN - 4 months ago 11
Swift Question

A guess that the Swift type alias mechanism is the automatic initializer Inheritance

The question popped in my head, what is happening when I define a Swift type alias? What is the mechanism behind it? Until I learned the Automatic Initializer Inheritance chapter from the Swift official document:


If your subclass doesn't define any designated initializer, it automatically inherits all of its superclass designated initializers


And here is my practice code for learning

class Vehicle{
var numberOfWheels = 0
var description: String {
return "\(numberOfWheels) wheel(s)"
}
}

class Auto: Vehicle{}

let VM = Auto()
VM.numberOfWheels


Wow! this works,at least performs, exactly as the Swift type alias. Auto is the alias of the type Vehicle

Question: Am I understand it right? This is the mechanism behind type alias.

Answer

Not really, but if you've never seen object oriented programming they could look somewhat similar, i agree.

Auto is a subclass that extends the original vehicle and could add additional properties and method to the Vehicle even if in that example it doesn't do it.

Auto and Vehicle are not the same thing, a Vehicle is a basic type and and Auto is one of its subtypes, what you can do with a Vehicle you can do with an Auto but not viceversa.

A typealias is just an alias, a way to give and additional "name" to a pre-existing type, just that. A type and his alias are the same thing.