[swift-evolution] Proposal Idea: Use of =?utf-8?Q?=3D=3D_?=outside of comparisons should be treated as an error.

Jared Sinclair desk at jaredsinclair.com
Mon Jan 11 14:39:42 CST 2016


Consider the following:

protocol Updatable {
    func update(state: Bool)
}

class Thing: Updatable {
    private var enabled = false
    
    func update(state: Bool) {
        self.enabled == state
    }
}

The obvious intention is to set self.enabled to the incoming value of state. However, it’s easy to accidentally type a second equals sign. The effect of this typo is self.enabled is never updated, leading to a run-time bug that could be difficult to diagnose. 

This typo doesn’t generate any errors or warnings. I can’t think of a valid reason to use the equals function outside of comparisons. If the compiler instead treated this typo as an error, the mistake would be trivial to identify and fix:

protocol Updatable {
    func update(state: Bool)
}

class Thing: Updatable {
    private var enabled = false
    
    func update(state: Bool) {
        self.enabled == state // Error: `==` may not be used outside of comparisons
    }
}

-- 
Jared Sinclair
Sent with Airmail
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160111/1f497d9f/attachment.html>


More information about the swift-evolution mailing list