Actually, there is no doubt that creating a typealias for -let's say- String: typealias MyString = String wouldn't be that useful, (I would also assume that declaring a typealias for Dictionary with specific key/value type: typealias CustomDict = Dictionary<String, Int> might not be that useful to you.
However, when it comes to work with compound types you would definitely notice the benefits of type aliasing.
Example:
Consider that you are implementing manager which repeatedly work with closures with many parameters in its functions:
class MyManager {
//...
func foo(success: (_ data: Data, _ message: String, _ status: Int, _ isEnabled: Bool) -> (), failure: (_ error: Error, _ message: String, _ workaround: AnyObject) -> ()) {
if isSuccess {
success(..., ..., ..., ...)
} else {
failure(..., ..., ...)
}
}
func bar(success: (_ data: Data, _ message: String, _ status: Int, _ isEnabled: Bool) -> (), failure: (_ error: Error, _ message: String, _ workaround: AnyObject) -> ()) {
if isSuccess {
success(..., ..., ..., ...)
} else {
failure(..., ..., ...)
}
}
// ...
}
As you can see, the methods signatures looks really tedious! both of the methods take success and failure parameters, each one of them are closures with arguments; Also, for implementing similar functions, it is not that logical to keep copy-paste the parameters.
Implementing typealias for such a case would be so appropriate:
class MyManager {
//...
typealias Success = (_ data: Data, _ message: String, _ status: Int, _ isEnabled: Bool) -> ()
typealias Failure = (_ error: Error, _ message: String, _ workaround: AnyObject) -> ()
func foo(success: Success, failure: Failure) {
if isSuccess {
success(..., ..., ..., ...)
} else {
failure(..., ..., ...)
}
}
func bar(success: Success, failure: Failure) {
if isSuccess {
success(..., ..., ..., ...)
} else {
failure(..., ..., ...)
}
}
// ...
}
Thus it would be more expressive and readable.
Furthermore, you might want to check a medium story I posted about it.
Videos
Actually, there is no doubt that creating a typealias for -let's say- String: typealias MyString = String wouldn't be that useful, (I would also assume that declaring a typealias for Dictionary with specific key/value type: typealias CustomDict = Dictionary<String, Int> might not be that useful to you.
However, when it comes to work with compound types you would definitely notice the benefits of type aliasing.
Example:
Consider that you are implementing manager which repeatedly work with closures with many parameters in its functions:
class MyManager {
//...
func foo(success: (_ data: Data, _ message: String, _ status: Int, _ isEnabled: Bool) -> (), failure: (_ error: Error, _ message: String, _ workaround: AnyObject) -> ()) {
if isSuccess {
success(..., ..., ..., ...)
} else {
failure(..., ..., ...)
}
}
func bar(success: (_ data: Data, _ message: String, _ status: Int, _ isEnabled: Bool) -> (), failure: (_ error: Error, _ message: String, _ workaround: AnyObject) -> ()) {
if isSuccess {
success(..., ..., ..., ...)
} else {
failure(..., ..., ...)
}
}
// ...
}
As you can see, the methods signatures looks really tedious! both of the methods take success and failure parameters, each one of them are closures with arguments; Also, for implementing similar functions, it is not that logical to keep copy-paste the parameters.
Implementing typealias for such a case would be so appropriate:
class MyManager {
//...
typealias Success = (_ data: Data, _ message: String, _ status: Int, _ isEnabled: Bool) -> ()
typealias Failure = (_ error: Error, _ message: String, _ workaround: AnyObject) -> ()
func foo(success: Success, failure: Failure) {
if isSuccess {
success(..., ..., ..., ...)
} else {
failure(..., ..., ...)
}
}
func bar(success: Success, failure: Failure) {
if isSuccess {
success(..., ..., ..., ...)
} else {
failure(..., ..., ...)
}
}
// ...
}
Thus it would be more expressive and readable.
Furthermore, you might want to check a medium story I posted about it.
The common way to use typealias for me is working with closures:
typealias VoidClosure = () -> Void
func updateFrom(completion: @escaping VoidClosure) { }
Hi
Im reading through the typing documentation https://docs.python.org/3/library/typing.html and have a question that I cannot answer.
In the past when I wanted to use type aliases I would use code like Vector = list[float] (I think that I must have picked this up from a post on stack overflow or something).
However, in the document above it suggests using the code type Vector = list[float].
The difference between the two is that the data type is types.GenericAlias (the list[float]) for the first Vector and typing.TypeAliasType for the second Vector.
But besides that I am not really sure what is the difference between these two methods. Im not sure where the reason to use one over the other is. Im also not sure where the documntation is for the first example (maybe technically this is not a Type Alias).
Im not sure if anyone can help here?
It’s often more readable to have dedicated names for function types, such as (Kitty) -> Unit. In the end, a typealias simply gives us a way to define an alias for such a (e.g.) function type which can then be used throughout the code instead of that explicit type as a 1:1 replacement.
In your example the function type (Kitty) -> Unit is being aliased with NewKittiesReceived, which is an argument to the shown function receiveNewKitties. Every variable holding a function (newKittiesReceived in the example) can be invoked with function.invoke(arguments...) or simpler with function(arguments).
There’s only a single situation in which the typealias is used actually: the definition of newKittiesReceived: NewKittiesReceived will be translated to newKittiesReceived: (Kitty) -> Unit.
The docs are found here.
This is what the documentation says:
Type aliases provide alternative names for existing types. If the type name is too long you can introduce a different shorter name and use the new one instead.
So, as you can see, typealises improve readability and shorten names that are to long.