Xcode-beta (7A152u): Shadowing seems to be implemented inconsistently

Originator:owensd
Number:rdar://21779311 Date Originated:10-Jul-2015 08:51 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode-beta (7A152u)
Classification:Other Bug Reproducible:Always
 
In a module, I’m able to create global functions that shadow functions from other modules.

See the following:

    public enum AssertionHandlers {
        // BUG(http://www.openradar.me/21776390): Cannot use @autoclosure, so evaluation will be forced.
        public typealias Handler = (Bool, String, file: StaticString, line: UWord) -> ()
        
        /// The handler associated with overriding the `assert` behavior.
        public static var assertionHandler: Handler? = nil
        
        /// The handler associated with overriding the `precondition` behavior.
        public static var preconditionHandler: Handler? = nil
    }

    /// This function shadows the `Swift.assert()` implementation in order to provide a mechanism to inject a
    /// different version of the `assert` implementation at runtime.
    public func assert(@autoclosure condition: () -> Bool, @autoclosure _ message: () -> String = "", file: StaticString = __FILE__, line: UWord = __LINE__)
    {
        if let handler = AssertionHandlers.assertionHandler {
            handler(condition(), message(), file: file, line: line)
        }
        else {
            Swift.assert(condition, message, file: file, line: line)
        }
    }

I am also able to shadow properties on types:

extension Array {
        var isEmpty: Bool {
            print("monkey patch!")
            return false
        }
    }

    let a: [Int] = []
    a.isEmpty  // outputs false and prints “monkey patch!”


However, functions in the extension have a compiler error when using them:

    extension Array {
        mutating func reserveCapacity(minimumCapacity: Int) {
            print("hi")
        }
    }

    var a: [Int] = []
    a.reserveCapacity(0) // error: ambiguous use of reserveCapacity()

It would seem to be advantageous to be able to shadow implementations with our for use within our own modules, however, this does not seem to be able to be done in a consistent manner. That seems like a bug to me.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!