Accessing shadowed swift var in extension: what should work? -
i have bunch of protocols designed let me mock out uikit stuff testing, among which:
public protocol haspresentedvctype { var presentedviewcontroller: haspresentedvctype? { } }
i cannot give uiviewcontroller
empty conformance protocol, because presentedviewcontroller
of type uiviewcontroller?
:
extension uiviewcontroller: haspresentedvctype {} // error:(32, 1) type 'uiviewcontroller' not conform protocol 'haspresentedvctype' // note:(18, 9) protocol requires property 'presentedviewcontroller' type 'haspresentedvctype?'
the following extension attempt shadow uiviewcontroller
's presentedviewcontroller
var
, let type inference choose whichever of 2 need:
extension uiviewcontroller: haspresentedvctype { public var presentedviewcontroller: haspresentedvctype? { let presented: uiviewcontroller? = self.presentedviewcontroller return presented } }
this works if extension not in same target original protocol, fails if protocol , extension in same target:
// error:(33, 16) 'presentedviewcontroller' used within own type
i can make work within same target introducing second protocol , putting implementation in constrained extension:
public protocol _haspresentedvctype { var presentedviewcontroller: uiviewcontroller? { } } extension uiviewcontroller: haspresentedvctype, _haspresentedvctype {} extension haspresentedvctype self: _haspresentedvctype { public var presentedviewcontroller: haspresentedvctype? { let presented: uiviewcontroller? = self.presentedviewcontroller return presented } }
so question: supposed work @ all, , if under circumstances?
(xcode 7.3, swift 2.2.)
and if (as rather suspect) it's not supposed work, there relatively tidy way similar effect?
Comments
Post a Comment