Re: access modifiers in protocols
Re: access modifiers in protocols
- Subject: Re: access modifiers in protocols
- From: Roland King <email@hidden>
- Date: Wed, 17 Jun 2015 16:34:06 +0800
> On 17 Jun 2015, at 14:12, Quincey Morris <email@hidden> wrote:
>
> On Jun 16, 2015, at 22:37 , Roland King <email@hidden <mailto:email@hidden>> wrote:
>>
>> No - can’t have a stored property in an extension
>
> (Someone just asked basically the same question in the dev forums, but with a different example. Was that you?)
>
> The following compiles for me without error:
>
>> public protocol Foo {
>> mutating func foo( Int )->Void
>> }
>>
>> extension Foo {
>> internal mutating func fooInternal( Int )->Void {}
>> public mutating func foo( i: Int )->Void { fooInternal (i)}
>> }
>>
>> internal protocol FooInternal: Foo {
>> mutating func fooInternal( Int )->Void
>> }
>>
>> internal protocol FooArray: FooInternal {
>> var bar : Array<Int> { get set }
>> }
>>
>> extension FooArray {
>> mutating func fooInternal( i: Int )->Void { bar.append( i )}
>> }
>>
>> public struct AFoo: Foo {
>> public mutating func foo( Int )->Void { print ("cheers")}
>> }
>>
>> public struct BFoo: Foo, FooArray {
>> var bar : Array<Int> = []
>> }
>
> I’m not absolutely sure it does what you want, and if it does I’m not sure it’s the shortest possible sequence. But it compiles without error.
>
Wasn’t me - how odd that two people were having the same thought at the same time. I find the new forum 2pt high pastel colours a bit hard to read so I’m not going there that often yet and having gone there I can’t find the question.
That’s a bit ugly, it name switches foo to fooInternal and puts a stubbed out version in the Foo extension - which was one of the things pointed out as ‘code smell’ in that talk. However it did turn on a light bulb and you can do this using the where clause in the extension
public protocol Foo
{
mutating func foo( Int ) -> Void
}
internal protocol FooArray : Foo
{
var bar : Array<Int> { get set }
}
extension Foo where Self : FooArray
{
public mutating func foo( i : Int ) -> Void
{
bar.append( i )
}
}
public struct S1 : FooArray
{
internal var bar : Array<Int> = []
}
//public struct S2 : Foo // should be an error as foo() isn’t implemented here
//{
//
//}
The stubbed out piece at the end should give a compilation error because S2 doesn’t implement Foo because it’s not a ‘FooArray’. However it just crashes the compiler. I’ll go file that one, I’ve found quite a few ways to crash it today, all doing things like this.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden