[swift-evolution] Pitch: Modify the meaning of IBOutlet to remove the ! from the type
michael.peternell at gmx.at
Wed May 18 15:16:36 CDT 2016
The really nice thing about the "!" operator is that it is always explicit. Either in the variable declaration, or when unwrapping it with "!" or "if let". "@IBOutlet weak var"s should therefore really not introduce an implicit "!". It's just one character, and it's good to see that this is actually an implicitly unwrapped optional. Furthermore, IOUs are made exactly for the case where you have a variable that *can* not be nil (unless you messed something up), but it cannot be proven by the compiler. If it is not a logic error if you try to use such a variable in code and it is nil, you should use a real optional instead. Furthermore, you can also write "@IBOutlet weak var foo: UIBar?" - both are valid. For example, a view controller can also do stuff if the view is not yet loaded.
and yes, "weak let" would break the semantic meaning of "let".
I think Optionals are really very very carefully designed already, and provide a simple(, useful) and consistent model. The description of [optionals, "!", "?", "if let", "weak", "unowned"] can be fully defined in a few sentences, and I'm against anything that just bloats this description without being an essential improvement.
> Am 18.05.2016 um 15:42 schrieb Vladimir.S via swift-evolution <swift-evolution at swift.org>:
> Correct me if I'm wrong, but `weak` reference can not be `let` just by definition: at some point in time such reference *can* become nil.
> On 18.05.2016 16:22, Daniel Steinberg via swift-evolution wrote:
>> I would like to propose we modify the meaning of @IBOutlet in light of the accepted proposal SE-0054 Abolish ImplicitlyUnwrappedOptional type. I think this fits in with the current Swift 3 goals.
>> Currently we use “var” and “!” when we declare an outlet like this:
>> @IBOutlet weak var myLabel: UILabel!
>> The “!” in the declaration allows us to use the outlet like this without unwrapping it
>> myLabel.text = “Hello"
>> We use “var” and “UILabel!" because myLabel starts its life out as nil and does not have a value until the connection is made. i.e. myLabel must be an optional if it accepts nil and the type is UILabel! instead of UILabel? so that we don’t have to unwrap it each time we use it.
>> If we break the connection to the UILabel instance we crash at runtime for attempting to unwrap nil.
>> Given this, I propose that we be able to write
>> @IBOutlet weak let myLabel: UILabel
>> In this case @IBOutlet has a meaning somewhat similar to lazy - it’s not that myLabel doesn’t exist until we first call it, but @IBOutlet indicates that myLabel should exist before we call it.
>> If the connection isn’t made and myLabel doesn’t exist, we should crash as we do now. If the connection is not made in the nib or storyboard, this will crash at development time.
>> This removes a case in which we use var - not because we want to change the value of a property but because of a detail in the tooling.
>> This change also removes a case in which we use an Optional again for a detail in lifecycle and tooling.
>> swift-evolution mailing list
>> swift-evolution at swift.org
> swift-evolution mailing list
> swift-evolution at swift.org
More information about the swift-evolution