Skip to content

Instantly share code, notes, and snippets.

@rydermackay
Created January 25, 2017 02:52
Show Gist options
  • Save rydermackay/85b354100e0236d082b604b8700ab0c2 to your computer and use it in GitHub Desktop.
Save rydermackay/85b354100e0236d082b604b8700ab0c2 to your computer and use it in GitHub Desktop.
// Xcode 8.3b1 (8W109m)
import Foundation
struct Box<T> {
let value: T
init(value: T) {
self.value = value
}
init(_ value: T) {
self.value = value
}
}
@objc protocol FooDelegate {
func foo(_ foo: Foo, didSomethingAt index: Int)
}
class Foo: NSObject {
weak var delegate: FooDelegate?
}
class UseFoo: NSObject {
var foo: Foo!
func referToMemberFooAsAny() {
foo = Foo()
foo.delegate = self
takesAny(foo) // self.foo
takesArray([foo]) // self.foo(_:didSomethingAt:)
takesArray([foo as Foo]) // self.foo
takesDict(["foo": foo]) // self.foo(_:didSomethingAt:) ?!
takesBox(Box(value: foo)) // self.foo(_:didSomethingAt:)
takesBox(Box(foo)) // self.foo !?!??!
}
func takesAny(_ value: Any) {
print(value)
}
func takesArray(_ array: Array<Any>) {
print(array)
}
func takesDict(_ dict: Dictionary<String, Any>) {
print(dict)
}
func takesBox(_ box: Box<Any>) {
print(box)
}
}
extension UseFoo: FooDelegate {
func foo(_ foo: Foo, didSomethingAt index: Int) {
print("foo \(foo) did something at \(index)")
}
}
@rydermackay
Copy link
Author

Xcode 8.2.1 and earlier infer self.foo in each case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment