Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining explicit conversion for custom types in Swift

What is currently the best/preferred way to define explicit conversions in Swift? Of the top of my head I can think of two:

  • Creating custom initializers for the destination type via an extension, like this:

    extension String {
      init(_ myType: MyType) {
        self = "Some Value"
      }
    }
    

    This way, you could just use String(m) where m is of type MyType to convert m to a string.

  • Defining toType-Methods in the source type, like this:

    class MyType {
      func toString() -> String {
        return "Some Value"
      }
    }
    

    This is comparable to Swift's String.toInt(), which returns an Int?. But if this was the definitive way to go I would expect there to be protocols for the basic types for this, like an inversion of the already existing *LiteralConvertible protocols.

Sub-question: None of the two methods allow something like this to compile: let s: MyString = myTypeInstance (as String) (part in parentheses optional), but if I understand right, the as operator is only for downcasting within type hierarchies, is that correct?

like image 807
Kolja Avatar asked Aug 30 '14 19:08

Kolja


People also ask

What is explicit type casting?

Explicit type conversion, also called type casting, is a type conversion which is explicitly defined within a program (instead of being done automatically according to the rules of the language for implicit type conversion). It is defined by the user in the program.

What are the different types of conversion?

There are two types of conversion: implicit and explicit. The term for implicit type conversion is coercion. Explicit type conversion in some specific way is known as casting.

What is type conversion explain its different types with examples?

Generally takes place when in an expression more than one data type is present. In such condition type conversion (type promotion) takes place to avoid loss of data. All the data types of the variables are upgraded to the data type of the variable with largest data type.

What is implicit conversion of data types?

Implicit Type Conversion is also known as 'automatic type conversion'. It is done by the compiler on its own, without any external trigger from the user. It generally takes place when in an expression more than one data type is present.


1 Answers

The pattern used in swift is the initializer. So for instance when converting an Int to UInt, we have to write:

var i: Int = 10
var u: UInt = UInt(i)

I would stick with that pattern.

As for the subquestion, the documentation states that:

Type casting is a way to check the type of an instance, and/or to treat that instance as if it is a different superclass or subclass from somewhere else in its own class hierarchy.

and

You can also use type casting to check whether a type conforms to a protocol

so no, the as keyword can`t be used to transform a value of a certain type to another type.

That can be tested in a simple way:

var i: Int = 10
var u: UInt = i as UInt

That generates an error:

'Int' is not convertible to 'UInt'

More about Type Casting

like image 150
Antonio Avatar answered Oct 21 '22 17:10

Antonio