Call protocol default implementation in inherence tree











up vote
1
down vote

favorite












This is a very similar question: Calling protocol default implementation from regular method, BUT none of the answers cover the case when inherance plays a role. The answer suggest casting it to the protocol type itself will solve it, but not in this case (Copy paste in playground):



public protocol MyProtocol {
func sayCheese()
}

public extension MyProtocol {
func sayHi() {
print("Hi, I am protocol")
sayCheese()
}

func sayCheese() {
print("Cheese from protocol")
}
}

public class MyFirstClass: MyProtocol {
public func sayCheese() {
print("Cheese from 1")

(self as MyProtocol).sayCheese()
}
}

public class MySecondClass: MyFirstClass {
override init() {
super.init()

sayHi()
}

public func sayHi() {
print("Hi from 2")
super.sayHi()
}

public override func sayCheese() {
print("Said cheese from 2")
super.sayCheese()
}
}

MySecondClass()


It prints the following:



...
Said cheese from 2
Cheese from 1
Said cheese from 2
Cheese from 1
...


In instance of MyFirstClass, how can I call the default implementation of MyProtocol of method sayCheese?



Edit: My usecase is the following:
I have a protocol which is adopted by a class, which is subclassed a lot. The protocol has multiple default methods, which can call eachother. Some of the subclasses needs to override the methods, do stuff, and call super.method() (to finally call the default implementation of the protocol, because a superclass may have overriden the protocol default implementation as well). So I really need dynamic dispatching.










share|improve this question
























  • You need to remove the sayCheese() requirement from the protocol to get the static dispatch that the answer to the question you linked relies on.
    – dan
    Nov 7 at 19:30










  • @dan See my edit
    – J. Doe
    Nov 7 at 19:43















up vote
1
down vote

favorite












This is a very similar question: Calling protocol default implementation from regular method, BUT none of the answers cover the case when inherance plays a role. The answer suggest casting it to the protocol type itself will solve it, but not in this case (Copy paste in playground):



public protocol MyProtocol {
func sayCheese()
}

public extension MyProtocol {
func sayHi() {
print("Hi, I am protocol")
sayCheese()
}

func sayCheese() {
print("Cheese from protocol")
}
}

public class MyFirstClass: MyProtocol {
public func sayCheese() {
print("Cheese from 1")

(self as MyProtocol).sayCheese()
}
}

public class MySecondClass: MyFirstClass {
override init() {
super.init()

sayHi()
}

public func sayHi() {
print("Hi from 2")
super.sayHi()
}

public override func sayCheese() {
print("Said cheese from 2")
super.sayCheese()
}
}

MySecondClass()


It prints the following:



...
Said cheese from 2
Cheese from 1
Said cheese from 2
Cheese from 1
...


In instance of MyFirstClass, how can I call the default implementation of MyProtocol of method sayCheese?



Edit: My usecase is the following:
I have a protocol which is adopted by a class, which is subclassed a lot. The protocol has multiple default methods, which can call eachother. Some of the subclasses needs to override the methods, do stuff, and call super.method() (to finally call the default implementation of the protocol, because a superclass may have overriden the protocol default implementation as well). So I really need dynamic dispatching.










share|improve this question
























  • You need to remove the sayCheese() requirement from the protocol to get the static dispatch that the answer to the question you linked relies on.
    – dan
    Nov 7 at 19:30










  • @dan See my edit
    – J. Doe
    Nov 7 at 19:43













up vote
1
down vote

favorite









up vote
1
down vote

favorite











This is a very similar question: Calling protocol default implementation from regular method, BUT none of the answers cover the case when inherance plays a role. The answer suggest casting it to the protocol type itself will solve it, but not in this case (Copy paste in playground):



public protocol MyProtocol {
func sayCheese()
}

public extension MyProtocol {
func sayHi() {
print("Hi, I am protocol")
sayCheese()
}

func sayCheese() {
print("Cheese from protocol")
}
}

public class MyFirstClass: MyProtocol {
public func sayCheese() {
print("Cheese from 1")

(self as MyProtocol).sayCheese()
}
}

public class MySecondClass: MyFirstClass {
override init() {
super.init()

sayHi()
}

public func sayHi() {
print("Hi from 2")
super.sayHi()
}

public override func sayCheese() {
print("Said cheese from 2")
super.sayCheese()
}
}

MySecondClass()


It prints the following:



...
Said cheese from 2
Cheese from 1
Said cheese from 2
Cheese from 1
...


In instance of MyFirstClass, how can I call the default implementation of MyProtocol of method sayCheese?



Edit: My usecase is the following:
I have a protocol which is adopted by a class, which is subclassed a lot. The protocol has multiple default methods, which can call eachother. Some of the subclasses needs to override the methods, do stuff, and call super.method() (to finally call the default implementation of the protocol, because a superclass may have overriden the protocol default implementation as well). So I really need dynamic dispatching.










share|improve this question















This is a very similar question: Calling protocol default implementation from regular method, BUT none of the answers cover the case when inherance plays a role. The answer suggest casting it to the protocol type itself will solve it, but not in this case (Copy paste in playground):



public protocol MyProtocol {
func sayCheese()
}

public extension MyProtocol {
func sayHi() {
print("Hi, I am protocol")
sayCheese()
}

func sayCheese() {
print("Cheese from protocol")
}
}

public class MyFirstClass: MyProtocol {
public func sayCheese() {
print("Cheese from 1")

(self as MyProtocol).sayCheese()
}
}

public class MySecondClass: MyFirstClass {
override init() {
super.init()

sayHi()
}

public func sayHi() {
print("Hi from 2")
super.sayHi()
}

public override func sayCheese() {
print("Said cheese from 2")
super.sayCheese()
}
}

MySecondClass()


It prints the following:



...
Said cheese from 2
Cheese from 1
Said cheese from 2
Cheese from 1
...


In instance of MyFirstClass, how can I call the default implementation of MyProtocol of method sayCheese?



Edit: My usecase is the following:
I have a protocol which is adopted by a class, which is subclassed a lot. The protocol has multiple default methods, which can call eachother. Some of the subclasses needs to override the methods, do stuff, and call super.method() (to finally call the default implementation of the protocol, because a superclass may have overriden the protocol default implementation as well). So I really need dynamic dispatching.







swift protocols






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 19:43

























asked Nov 7 at 19:18









J. Doe

2,6451931




2,6451931












  • You need to remove the sayCheese() requirement from the protocol to get the static dispatch that the answer to the question you linked relies on.
    – dan
    Nov 7 at 19:30










  • @dan See my edit
    – J. Doe
    Nov 7 at 19:43


















  • You need to remove the sayCheese() requirement from the protocol to get the static dispatch that the answer to the question you linked relies on.
    – dan
    Nov 7 at 19:30










  • @dan See my edit
    – J. Doe
    Nov 7 at 19:43
















You need to remove the sayCheese() requirement from the protocol to get the static dispatch that the answer to the question you linked relies on.
– dan
Nov 7 at 19:30




You need to remove the sayCheese() requirement from the protocol to get the static dispatch that the answer to the question you linked relies on.
– dan
Nov 7 at 19:30












@dan See my edit
– J. Doe
Nov 7 at 19:43




@dan See my edit
– J. Doe
Nov 7 at 19:43












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Although protocols support concrete implementations using extension but that defies the concept of a protocol




A protocol defines a blueprint of methods, properties, and other
requirements that suit a particular task or piece of functionality.




and it should be avoided as much as possible until you are truly sure this will not be overriden.



Solution 1:



If you have been into a situation mentioned then i would suggest to introduce an intermediary class that does nothing except conforming to that protocol and then inherit subClasses from that class.



public class Intermediary: MyProtocol {}

public class MyFirstClass: Intermediary {
public func sayCheese() {
print("Cheese from 1")

super.sayCheese()
}
}

public class MySecondClass: MyFirstClass {
override init() {
super.init()

sayHi()
}

public func sayHi() {
print("Hi from 2")
super.sayHi()
}

public override func sayCheese() {
print("Said cheese from 2")
super.sayCheese()
}
}


Now creating object MySecondClass() will print the below output,



Hi from 2
Hi, I am protocol
Cheese from protocol




Solution 2:



As mentioned in the other question, remove sayCheese method from the protocol so your protocol declaration will be empty but sayCheese will stay in the extension and it will break the recursive calls to sayCheese allowing your app to not freeze.



public protocol MyProtocol {}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196318%2fcall-protocol-default-implementation-in-inherence-tree%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Although protocols support concrete implementations using extension but that defies the concept of a protocol




    A protocol defines a blueprint of methods, properties, and other
    requirements that suit a particular task or piece of functionality.




    and it should be avoided as much as possible until you are truly sure this will not be overriden.



    Solution 1:



    If you have been into a situation mentioned then i would suggest to introduce an intermediary class that does nothing except conforming to that protocol and then inherit subClasses from that class.



    public class Intermediary: MyProtocol {}

    public class MyFirstClass: Intermediary {
    public func sayCheese() {
    print("Cheese from 1")

    super.sayCheese()
    }
    }

    public class MySecondClass: MyFirstClass {
    override init() {
    super.init()

    sayHi()
    }

    public func sayHi() {
    print("Hi from 2")
    super.sayHi()
    }

    public override func sayCheese() {
    print("Said cheese from 2")
    super.sayCheese()
    }
    }


    Now creating object MySecondClass() will print the below output,



    Hi from 2
    Hi, I am protocol
    Cheese from protocol




    Solution 2:



    As mentioned in the other question, remove sayCheese method from the protocol so your protocol declaration will be empty but sayCheese will stay in the extension and it will break the recursive calls to sayCheese allowing your app to not freeze.



    public protocol MyProtocol {}





    share|improve this answer



























      up vote
      1
      down vote



      accepted










      Although protocols support concrete implementations using extension but that defies the concept of a protocol




      A protocol defines a blueprint of methods, properties, and other
      requirements that suit a particular task or piece of functionality.




      and it should be avoided as much as possible until you are truly sure this will not be overriden.



      Solution 1:



      If you have been into a situation mentioned then i would suggest to introduce an intermediary class that does nothing except conforming to that protocol and then inherit subClasses from that class.



      public class Intermediary: MyProtocol {}

      public class MyFirstClass: Intermediary {
      public func sayCheese() {
      print("Cheese from 1")

      super.sayCheese()
      }
      }

      public class MySecondClass: MyFirstClass {
      override init() {
      super.init()

      sayHi()
      }

      public func sayHi() {
      print("Hi from 2")
      super.sayHi()
      }

      public override func sayCheese() {
      print("Said cheese from 2")
      super.sayCheese()
      }
      }


      Now creating object MySecondClass() will print the below output,



      Hi from 2
      Hi, I am protocol
      Cheese from protocol




      Solution 2:



      As mentioned in the other question, remove sayCheese method from the protocol so your protocol declaration will be empty but sayCheese will stay in the extension and it will break the recursive calls to sayCheese allowing your app to not freeze.



      public protocol MyProtocol {}





      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Although protocols support concrete implementations using extension but that defies the concept of a protocol




        A protocol defines a blueprint of methods, properties, and other
        requirements that suit a particular task or piece of functionality.




        and it should be avoided as much as possible until you are truly sure this will not be overriden.



        Solution 1:



        If you have been into a situation mentioned then i would suggest to introduce an intermediary class that does nothing except conforming to that protocol and then inherit subClasses from that class.



        public class Intermediary: MyProtocol {}

        public class MyFirstClass: Intermediary {
        public func sayCheese() {
        print("Cheese from 1")

        super.sayCheese()
        }
        }

        public class MySecondClass: MyFirstClass {
        override init() {
        super.init()

        sayHi()
        }

        public func sayHi() {
        print("Hi from 2")
        super.sayHi()
        }

        public override func sayCheese() {
        print("Said cheese from 2")
        super.sayCheese()
        }
        }


        Now creating object MySecondClass() will print the below output,



        Hi from 2
        Hi, I am protocol
        Cheese from protocol




        Solution 2:



        As mentioned in the other question, remove sayCheese method from the protocol so your protocol declaration will be empty but sayCheese will stay in the extension and it will break the recursive calls to sayCheese allowing your app to not freeze.



        public protocol MyProtocol {}





        share|improve this answer














        Although protocols support concrete implementations using extension but that defies the concept of a protocol




        A protocol defines a blueprint of methods, properties, and other
        requirements that suit a particular task or piece of functionality.




        and it should be avoided as much as possible until you are truly sure this will not be overriden.



        Solution 1:



        If you have been into a situation mentioned then i would suggest to introduce an intermediary class that does nothing except conforming to that protocol and then inherit subClasses from that class.



        public class Intermediary: MyProtocol {}

        public class MyFirstClass: Intermediary {
        public func sayCheese() {
        print("Cheese from 1")

        super.sayCheese()
        }
        }

        public class MySecondClass: MyFirstClass {
        override init() {
        super.init()

        sayHi()
        }

        public func sayHi() {
        print("Hi from 2")
        super.sayHi()
        }

        public override func sayCheese() {
        print("Said cheese from 2")
        super.sayCheese()
        }
        }


        Now creating object MySecondClass() will print the below output,



        Hi from 2
        Hi, I am protocol
        Cheese from protocol




        Solution 2:



        As mentioned in the other question, remove sayCheese method from the protocol so your protocol declaration will be empty but sayCheese will stay in the extension and it will break the recursive calls to sayCheese allowing your app to not freeze.



        public protocol MyProtocol {}






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 8 at 8:27

























        answered Nov 8 at 4:56









        Kamran

        5,09011027




        5,09011027






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196318%2fcall-protocol-default-implementation-in-inherence-tree%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()