I like the question. Only the Swift command could finally answer, but I can assume why: converting a typed value to a variable of another type without explicit conversion or cast is very easy to confuse with a programmer error, and in many cases this is something the compiler should warn.
Example (and suppose Person also a StringLiteralConvertible , which can be initialized with a string variable as well as a literal, as you present in your question):
struct Person { private static var idCounter = 1 var name:String let id:Int init(withName name:String) { Person.idCounter += 1 self.name = name self.id = Person.idCounter } } var person = Person(withName:"Mary") let name = "John" person = name
The above code looks suspiciously like an error when a programmer assigns a value of the wrong type ( String ) to a variable of type Person . This may actually be a mistake. Maybe the programmer just wanted to change the personβs name ( person.name = name ) without creating a new Person with a new unique id. Or perhaps the programmer intended to assign a different value to Person , but made a typo error or a code completion error. It is difficult to say, not being the original programmer, or to carefully study the entire context to understand whether this transformation makes sense. And it gets harder and harder the more the assignment comes from where the variables are initially initialized. If the compiler warns here that a value of type String assigned to a variable of type Person ?
The example will be much clearer and more consistent with Swift conventions like:
var person = Person(withName:"Mary") let name = "John" person = Person(withName:name)
The above version is completely unique, both for the compiler and for any other programmers who read this later.
source share