Attempting to call default protocol implementation crashes Playground


(Rick M) #1

The following gives Xcode 8.1 a very hard time. Eventually I get a Bad Access on the last line. I'm guessing it's a recursive call. Is there any way to call the default implementation from a "real" implementation?

protocol FooPro
{
  func fooFunc()
}

extension FooPro
{
  func
  fooFunc()
  {
    print("fooFunc default")
  }
}

class FooClass : FooPro
{
  func
  fooFunc()
  {
    (self as FooPro).fooFunc()
    print("fooFunc FooClass")
  }
}

let fc: FooPro = FooClass()
fc.fooFunc()

Thanks!

···

--
Rick Mann
rmann@latencyzero.com


(Dan Loewenherz) #2

What are you trying to accomplish here, more concretely?

My first thought is that you shouldn't implement the same function in both
a protocol extension and a conforming class. Why not just give them
different names and call the function from within the extension instead of
from the class? E.g.

protocol FooPro {

    func _fooFunc()

}

extension FooPro {

    func fooFunc() {

        print("fooFunc default")

        _fooFunc()

    }

}

class FooClass: FooPro {

    func _fooFunc() {

        print("fooFunc FooClass")

    }

}

let fc = FooClass()

fc.fooFunc()

Dan

···

On Tue, Nov 15, 2016 at 4:28 PM, Rick Mann via swift-users < swift-users@swift.org> wrote:

The following gives Xcode 8.1 a very hard time. Eventually I get a Bad
Access on the last line. I'm guessing it's a recursive call. Is there any
way to call the default implementation from a "real" implementation?

protocol FooPro
{
        func fooFunc()
}

extension FooPro
{
        func
        fooFunc()
        {
                print("fooFunc default")
        }
}

class FooClass : FooPro
{
        func
        fooFunc()
        {
                (self as FooPro).fooFunc()
                print("fooFunc FooClass")
        }
}

let fc: FooPro = FooClass()
fc.fooFunc()

Thanks!

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Rick M) #3

Well, this is a standard protocol default implementation. I was experimenting to see if it was possible to call the default implementation after providing a concrete implementation.

···

On Nov 15, 2016, at 14:47 , Dan Loewenherz <dan@lionheartsw.com> wrote:

What are you trying to accomplish here, more concretely?

My first thought is that you shouldn't implement the same function in both a protocol extension and a conforming class. Why not just give them different names and call the function from within the extension instead of from the class? E.g.

protocol FooPro {
    func _fooFunc()
}

extension FooPro {
    func fooFunc() {
        print("fooFunc default")
        _fooFunc()
    }
}

class FooClass: FooPro {
    func _fooFunc() {
        print("fooFunc FooClass")
    }
}

let fc = FooClass()
fc.fooFunc()

Dan

On Tue, Nov 15, 2016 at 4:28 PM, Rick Mann via swift-users <swift-users@swift.org> wrote:
The following gives Xcode 8.1 a very hard time. Eventually I get a Bad Access on the last line. I'm guessing it's a recursive call. Is there any way to call the default implementation from a "real" implementation?

protocol FooPro
{
        func fooFunc()
}

extension FooPro
{
        func
        fooFunc()
        {
                print("fooFunc default")
        }
}

class FooClass : FooPro
{
        func
        fooFunc()
        {
                (self as FooPro).fooFunc()
                print("fooFunc FooClass")
        }
}

let fc: FooPro = FooClass()
fc.fooFunc()

Thanks!

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Rick Mann
rmann@latencyzero.com


(Zhao Xin) #4

'Default' implementation in protocol extension is used as fail safe. You should not consider it like something super class does. If you want it that way, use class inheritance instead.
Zhaoxin

···

Get Outlook for iOS

    _____________________________
From: Rick Mann via swift-users <swift-users@swift.org>
Sent: 星期三, 十一月 16, 2016 07:51
Subject: Re: [swift-users] Attempting to call default protocol implementation crashes Playground
To: Dan Loewenherz <dan@lionheartsw.com>
Cc: swift-users <swift-users@swift.org>

Well, this is a standard protocol default implementation. I was experimenting to see if it was possible to call the default implementation after providing a concrete implementation.

On Nov 15, 2016, at 14:47 , Dan Loewenherz <dan@lionheartsw.com> wrote:

What are you trying to accomplish here, more concretely?

My first thought is that you shouldn't implement the same function in both a protocol extension and a conforming class. Why not just give them different names and call the function from within the extension instead of from the class? E.g.

protocol FooPro {
    func _fooFunc()
}

extension FooPro {
    func fooFunc() {
        print("fooFunc default")
        _fooFunc()
    }
}

class FooClass: FooPro {
    func _fooFunc() {
        print("fooFunc FooClass")
    }
}

let fc = FooClass()
fc.fooFunc()

Dan

On Tue, Nov 15, 2016 at 4:28 PM, Rick Mann via swift-users <swift-users@swift.org> wrote:
The following gives Xcode 8.1 a very hard time. Eventually I get a Bad Access on the last line. I'm guessing it's a recursive call. Is there any way to call the default implementation from a "real" implementation?

protocol FooPro
{
        func fooFunc()
}

extension FooPro
{
        func
        fooFunc()
        {
                print("fooFunc default")
        }
}

class FooClass : FooPro
{
        func
        fooFunc()
        {
                (self as FooPro).fooFunc()
                print("fooFunc FooClass")
        }
}

let fc: FooPro = FooClass()
fc.fooFunc()

Thanks!

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Rick M) #5

Okay. I coudln't find official documentation on this, and I don't currently need to do this, but wanted to fully understand it.

···

On Nov 15, 2016, at 17:27 , zh ao <owenzx@gmail.com> wrote:

'Default' implementation in protocol extension is used as fail safe. You should not consider it like something super class does. If you want it that way, use class inheritance instead.

Zhaoxin

Get Outlook for iOS

_____________________________
From: Rick Mann via swift-users <swift-users@swift.org>
Sent: 星期三, 十一月 16, 2016 07:51
Subject: Re: [swift-users] Attempting to call default protocol implementation crashes Playground
To: Dan Loewenherz <dan@lionheartsw.com>
Cc: swift-users <swift-users@swift.org>

Well, this is a standard protocol default implementation. I was experimenting to see if it was possible to call the default implementation after providing a concrete implementation.

> On Nov 15, 2016, at 14:47 , Dan Loewenherz <dan@lionheartsw.com> wrote:
>
> What are you trying to accomplish here, more concretely?
>
> My first thought is that you shouldn't implement the same function in both a protocol extension and a conforming class. Why not just give them different names and call the function from within the extension instead of from the class? E.g.
>
> protocol FooPro {
> func _fooFunc()
> }
>
> extension FooPro {
> func fooFunc() {
> print("fooFunc default")
> _fooFunc()
> }
> }
>
> class FooClass: FooPro {
> func _fooFunc() {
> print("fooFunc FooClass")
> }
> }
>
> let fc = FooClass()
> fc.fooFunc()
>
> Dan
>
> On Tue, Nov 15, 2016 at 4:28 PM, Rick Mann via swift-users <swift-users@swift.org> wrote:
> The following gives Xcode 8.1 a very hard time. Eventually I get a Bad Access on the last line. I'm guessing it's a recursive call. Is there any way to call the default implementation from a "real" implementation?
>
> protocol FooPro
> {
> func fooFunc()
> }
>
> extension FooPro
> {
> func
> fooFunc()
> {
> print("fooFunc default")
> }
> }
>
> class FooClass : FooPro
> {
> func
> fooFunc()
> {
> (self as FooPro).fooFunc()
> print("fooFunc FooClass")
> }
> }
>
> let fc: FooPro = FooClass()
> fc.fooFunc()
>
>
> Thanks!
>
>
> --
> Rick Mann
> rmann@latencyzero.com
>
>
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Rick Mann
rmann@latencyzero.com


(Игорь Никитин) #6

Right, it’s a recursion, because this

(self as FooPro).fooFunc()

Will call FooClass’s method implementation

You can read more about dispatch rules here: https://medium.com/ios-os-x-development/swift-protocol-extension-method-dispatch-6a6bf270ba94#.hkh1rc56p

···

16 нояб. 2016 г., в 4:29, Rick Mann via swift-users <swift-users@swift.org> написал(а):

Okay. I coudln't find official documentation on this, and I don't currently need to do this, but wanted to fully understand it.

On Nov 15, 2016, at 17:27 , zh ao <owenzx@gmail.com> wrote:

'Default' implementation in protocol extension is used as fail safe. You should not consider it like something super class does. If you want it that way, use class inheritance instead.

Zhaoxin

Get Outlook for iOS

_____________________________
From: Rick Mann via swift-users <swift-users@swift.org>
Sent: 星期三, 十一月 16, 2016 07:51
Subject: Re: [swift-users] Attempting to call default protocol implementation crashes Playground
To: Dan Loewenherz <dan@lionheartsw.com>
Cc: swift-users <swift-users@swift.org>

Well, this is a standard protocol default implementation. I was experimenting to see if it was possible to call the default implementation after providing a concrete implementation.

On Nov 15, 2016, at 14:47 , Dan Loewenherz <dan@lionheartsw.com> wrote:

What are you trying to accomplish here, more concretely?

My first thought is that you shouldn't implement the same function in both a protocol extension and a conforming class. Why not just give them different names and call the function from within the extension instead of from the class? E.g.

protocol FooPro {
func _fooFunc()
}

extension FooPro {
func fooFunc() {
print("fooFunc default")
_fooFunc()
}
}

class FooClass: FooPro {
func _fooFunc() {
print("fooFunc FooClass")
}
}

let fc = FooClass()
fc.fooFunc()

Dan

On Tue, Nov 15, 2016 at 4:28 PM, Rick Mann via swift-users <swift-users@swift.org> wrote:
The following gives Xcode 8.1 a very hard time. Eventually I get a Bad Access on the last line. I'm guessing it's a recursive call. Is there any way to call the default implementation from a "real" implementation?

protocol FooPro
{
func fooFunc()
}

extension FooPro
{
func
fooFunc()
{
print("fooFunc default")
}
}

class FooClass : FooPro
{
func
fooFunc()
{
(self as FooPro).fooFunc()
print("fooFunc FooClass")
}
}

let fc: FooPro = FooClass()
fc.fooFunc()

Thanks!

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Rick Mann
rmann@latencyzero.com <mailto:rmann@latencyzero.com>

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users


(Игорь Никитин) #7

And, as I know, it's not possible to call protocol's implementation in that case

···

16 нояб. 2016 г., в 4:29, Rick Mann via swift-users <swift-users@swift.org> написал(а):

Okay. I coudln't find official documentation on this, and I don't currently need to do this, but wanted to fully understand it.

On Nov 15, 2016, at 17:27 , zh ao <owenzx@gmail.com> wrote:

'Default' implementation in protocol extension is used as fail safe. You should not consider it like something super class does. If you want it that way, use class inheritance instead.

Zhaoxin

Get Outlook for iOS

_____________________________
From: Rick Mann via swift-users <swift-users@swift.org>
Sent: 星期三, 十一月 16, 2016 07:51
Subject: Re: [swift-users] Attempting to call default protocol implementation crashes Playground
To: Dan Loewenherz <dan@lionheartsw.com>
Cc: swift-users <swift-users@swift.org>

Well, this is a standard protocol default implementation. I was experimenting to see if it was possible to call the default implementation after providing a concrete implementation.

On Nov 15, 2016, at 14:47 , Dan Loewenherz <dan@lionheartsw.com> wrote:

What are you trying to accomplish here, more concretely?

My first thought is that you shouldn't implement the same function in both a protocol extension and a conforming class. Why not just give them different names and call the function from within the extension instead of from the class? E.g.

protocol FooPro {
func _fooFunc()
}

extension FooPro {
func fooFunc() {
print("fooFunc default")
_fooFunc()
}
}

class FooClass: FooPro {
func _fooFunc() {
print("fooFunc FooClass")
}
}

let fc = FooClass()
fc.fooFunc()

Dan

On Tue, Nov 15, 2016 at 4:28 PM, Rick Mann via swift-users <swift-users@swift.org> wrote:
The following gives Xcode 8.1 a very hard time. Eventually I get a Bad Access on the last line. I'm guessing it's a recursive call. Is there any way to call the default implementation from a "real" implementation?

protocol FooPro
{
func fooFunc()
}

extension FooPro
{
func
fooFunc()
{
print("fooFunc default")
}
}

class FooClass : FooPro
{
func
fooFunc()
{
(self as FooPro).fooFunc()
print("fooFunc FooClass")
}
}

let fc: FooPro = FooClass()
fc.fooFunc()

Thanks!

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Игорь Никитин) #8

And, as I know, it's not possible to call protocol's implementation in that case

···

16 нояб. 2016 г., в 9:09, Игорь Никитин via swift-users <swift-users@swift.org> написал(а):

Right, it’s a recursion, because this

(self as FooPro).fooFunc()

Will call FooClass’s method implementation

You can read more about dispatch rules here: https://medium.com/ios-os-x-development/swift-protocol-extension-method-dispatch-6a6bf270ba94#.hkh1rc56p

16 нояб. 2016 г., в 4:29, Rick Mann via swift-users <swift-users@swift.org> написал(а):

Okay. I coudln't find official documentation on this, and I don't currently need to do this, but wanted to fully understand it.

On Nov 15, 2016, at 17:27 , zh ao <owenzx@gmail.com> wrote:

'Default' implementation in protocol extension is used as fail safe. You should not consider it like something super class does. If you want it that way, use class inheritance instead.

Zhaoxin

Get Outlook for iOS

_____________________________
From: Rick Mann via swift-users <swift-users@swift.org>
Sent: 星期三, 十一月 16, 2016 07:51
Subject: Re: [swift-users] Attempting to call default protocol implementation crashes Playground
To: Dan Loewenherz <dan@lionheartsw.com>
Cc: swift-users <swift-users@swift.org>

Well, this is a standard protocol default implementation. I was experimenting to see if it was possible to call the default implementation after providing a concrete implementation.

On Nov 15, 2016, at 14:47 , Dan Loewenherz <dan@lionheartsw.com> wrote:

What are you trying to accomplish here, more concretely?

My first thought is that you shouldn't implement the same function in both a protocol extension and a conforming class. Why not just give them different names and call the function from within the extension instead of from the class? E.g.

protocol FooPro {
func _fooFunc()
}

extension FooPro {
func fooFunc() {
print("fooFunc default")
_fooFunc()
}
}

class FooClass: FooPro {
func _fooFunc() {
print("fooFunc FooClass")
}
}

let fc = FooClass()
fc.fooFunc()

Dan

On Tue, Nov 15, 2016 at 4:28 PM, Rick Mann via swift-users <swift-users@swift.org> wrote:
The following gives Xcode 8.1 a very hard time. Eventually I get a Bad Access on the last line. I'm guessing it's a recursive call. Is there any way to call the default implementation from a "real" implementation?

protocol FooPro
{
func fooFunc()
}

extension FooPro
{
func
fooFunc()
{
print("fooFunc default")
}
}

class FooClass : FooPro
{
func
fooFunc()
{
(self as FooPro).fooFunc()
print("fooFunc FooClass")
}
}

let fc: FooPro = FooClass()
fc.fooFunc()

Thanks!

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users