Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use typealias?

So far I understand that the typealias is a named alias of an existing type. By using typealias, I could do something like:

typealias MyString = String
var str: MyString?

typealias Strings = [String]
var strs: Strings?

which leads to declare str variable as String and strs as an Array of strings.

Even for a custom type:

class MyClass {}
typealias MyClsType = MyClass
var myClass: MyClsType

However, it seems a little unuseful; Logically, what is the purpose of declaring -for example- var str: MyString? to be a String instead of var str: String?? even more, var str: String is more expressive.

like image 532
Ahmad F Avatar asked Nov 21 '17 22:11

Ahmad F


People also ask

What is the use of Typealias?

typealias can be useful for reducing verbosity and complexity and improving readability and clarity. typealias is especially powerful when working with complex closure types and those conforming to multiple protocols.

Why Typealias is used in Swift?

Swift Typealias is used to provide a new name for an existing data type in the program. Once you create a typealias, you can use the aliased name instead of the exsisting name throughout the program. Typealias doesn't create a new data type, it simply provides a new name to the existing data type.

What is a Typealias in Kotlin?

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. It's useful to shorten long generic types. For instance, it's often tempting to shrink collection types: typealias NodeSet = Set<Network.

How do I declare Typealias in Swift?

A typealias can be declared in Swift using the typealias keyword followed by the type you want to assign. A very simple example to understand how they can be used is by making an alias for currency, like Dollars.


2 Answers

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.

like image 57
Ahmad F Avatar answered Oct 02 '22 19:10

Ahmad F


The common way to use typealias for me is working with closures:

typealias VoidClosure = () -> Void

func updateFrom(completion: @escaping VoidClosure) { }

like image 29
Serg Tsarikovskiy Avatar answered Oct 02 '22 20:10

Serg Tsarikovskiy