Multiple Equatable functions in one class









up vote
1
down vote

favorite












I would like to know if its possible to have multiple Equatable functions in one class. My current problem is from this example:



class Ingredient: Equatable 

static func == (lhs: Ingredient, rhs: Ingredient) -> Bool
return lhs.ingredientId == rhs.ingredientId && lhs.price == rhs.price


// static func == (lhs: Ingredient, rhs: Ingredient) -> Bool
// return lhs.ingredientId == rhs.ingredientId && lhs.price == rhs.price && lhs.quantity == rhs.quantity
//



As you can see I would like to have commented equality function but it doesn't work this way because I already have one. I need both functions working. Is there any optimal solution for this?










share|improve this question























  • Optimal solution? The optional solution is to determine what uniquely identifies an object and use that when implementing Equatable and then use your own "specialised" functions for any other type of comparison, perhaps create a protocol to define them if they are to be implemented for several classes
    – Joakim Danielson
    Nov 11 at 15:02






  • 1




    How would that make any sense? Which function do you want Swift to use to determine equality? Either the quantity matters or it doesn't.
    – vacawama
    Nov 11 at 15:32














up vote
1
down vote

favorite












I would like to know if its possible to have multiple Equatable functions in one class. My current problem is from this example:



class Ingredient: Equatable 

static func == (lhs: Ingredient, rhs: Ingredient) -> Bool
return lhs.ingredientId == rhs.ingredientId && lhs.price == rhs.price


// static func == (lhs: Ingredient, rhs: Ingredient) -> Bool
// return lhs.ingredientId == rhs.ingredientId && lhs.price == rhs.price && lhs.quantity == rhs.quantity
//



As you can see I would like to have commented equality function but it doesn't work this way because I already have one. I need both functions working. Is there any optimal solution for this?










share|improve this question























  • Optimal solution? The optional solution is to determine what uniquely identifies an object and use that when implementing Equatable and then use your own "specialised" functions for any other type of comparison, perhaps create a protocol to define them if they are to be implemented for several classes
    – Joakim Danielson
    Nov 11 at 15:02






  • 1




    How would that make any sense? Which function do you want Swift to use to determine equality? Either the quantity matters or it doesn't.
    – vacawama
    Nov 11 at 15:32












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I would like to know if its possible to have multiple Equatable functions in one class. My current problem is from this example:



class Ingredient: Equatable 

static func == (lhs: Ingredient, rhs: Ingredient) -> Bool
return lhs.ingredientId == rhs.ingredientId && lhs.price == rhs.price


// static func == (lhs: Ingredient, rhs: Ingredient) -> Bool
// return lhs.ingredientId == rhs.ingredientId && lhs.price == rhs.price && lhs.quantity == rhs.quantity
//



As you can see I would like to have commented equality function but it doesn't work this way because I already have one. I need both functions working. Is there any optimal solution for this?










share|improve this question















I would like to know if its possible to have multiple Equatable functions in one class. My current problem is from this example:



class Ingredient: Equatable 

static func == (lhs: Ingredient, rhs: Ingredient) -> Bool
return lhs.ingredientId == rhs.ingredientId && lhs.price == rhs.price


// static func == (lhs: Ingredient, rhs: Ingredient) -> Bool
// return lhs.ingredientId == rhs.ingredientId && lhs.price == rhs.price && lhs.quantity == rhs.quantity
//



As you can see I would like to have commented equality function but it doesn't work this way because I already have one. I need both functions working. Is there any optimal solution for this?







swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 18:10









rmaddy

236k27307374




236k27307374










asked Nov 11 at 14:45









Marek Baláž

62




62











  • Optimal solution? The optional solution is to determine what uniquely identifies an object and use that when implementing Equatable and then use your own "specialised" functions for any other type of comparison, perhaps create a protocol to define them if they are to be implemented for several classes
    – Joakim Danielson
    Nov 11 at 15:02






  • 1




    How would that make any sense? Which function do you want Swift to use to determine equality? Either the quantity matters or it doesn't.
    – vacawama
    Nov 11 at 15:32
















  • Optimal solution? The optional solution is to determine what uniquely identifies an object and use that when implementing Equatable and then use your own "specialised" functions for any other type of comparison, perhaps create a protocol to define them if they are to be implemented for several classes
    – Joakim Danielson
    Nov 11 at 15:02






  • 1




    How would that make any sense? Which function do you want Swift to use to determine equality? Either the quantity matters or it doesn't.
    – vacawama
    Nov 11 at 15:32















Optimal solution? The optional solution is to determine what uniquely identifies an object and use that when implementing Equatable and then use your own "specialised" functions for any other type of comparison, perhaps create a protocol to define them if they are to be implemented for several classes
– Joakim Danielson
Nov 11 at 15:02




Optimal solution? The optional solution is to determine what uniquely identifies an object and use that when implementing Equatable and then use your own "specialised" functions for any other type of comparison, perhaps create a protocol to define them if they are to be implemented for several classes
– Joakim Danielson
Nov 11 at 15:02




1




1




How would that make any sense? Which function do you want Swift to use to determine equality? Either the quantity matters or it doesn't.
– vacawama
Nov 11 at 15:32




How would that make any sense? Which function do you want Swift to use to determine equality? Either the quantity matters or it doesn't.
– vacawama
Nov 11 at 15:32












2 Answers
2






active

oldest

votes

















up vote
2
down vote













No, you can not.



From the math definition of the function:




A function is a process or a relation that associates each element x of a set X, the domain of the function, to a single element y of another set Y (possibly the same set), the codomain of the function.




In general, by defining more than one function with the same name which takes arguments of the same types you create ambiguity which leads to unpredictable results. So this behaviour will generate compile time error.






share|improve this answer





























    up vote
    1
    down vote













    You can't have multiple definitions of the ==() function (the function that gives you conformance to the equatable protocol) but you can write a complex implementation of your ==() function that uses more complex logic to decide how to compare two objects. You could even add a class variable to your class and have your ==() function use that variable in deciding how to compare values, although that seems like bad design.






    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%2f53249828%2fmultiple-equatable-functions-in-one-class%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      No, you can not.



      From the math definition of the function:




      A function is a process or a relation that associates each element x of a set X, the domain of the function, to a single element y of another set Y (possibly the same set), the codomain of the function.




      In general, by defining more than one function with the same name which takes arguments of the same types you create ambiguity which leads to unpredictable results. So this behaviour will generate compile time error.






      share|improve this answer


























        up vote
        2
        down vote













        No, you can not.



        From the math definition of the function:




        A function is a process or a relation that associates each element x of a set X, the domain of the function, to a single element y of another set Y (possibly the same set), the codomain of the function.




        In general, by defining more than one function with the same name which takes arguments of the same types you create ambiguity which leads to unpredictable results. So this behaviour will generate compile time error.






        share|improve this answer
























          up vote
          2
          down vote










          up vote
          2
          down vote









          No, you can not.



          From the math definition of the function:




          A function is a process or a relation that associates each element x of a set X, the domain of the function, to a single element y of another set Y (possibly the same set), the codomain of the function.




          In general, by defining more than one function with the same name which takes arguments of the same types you create ambiguity which leads to unpredictable results. So this behaviour will generate compile time error.






          share|improve this answer














          No, you can not.



          From the math definition of the function:




          A function is a process or a relation that associates each element x of a set X, the domain of the function, to a single element y of another set Y (possibly the same set), the codomain of the function.




          In general, by defining more than one function with the same name which takes arguments of the same types you create ambiguity which leads to unpredictable results. So this behaviour will generate compile time error.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 14:57

























          answered Nov 11 at 14:50









          fewlinesofcode

          1,900515




          1,900515






















              up vote
              1
              down vote













              You can't have multiple definitions of the ==() function (the function that gives you conformance to the equatable protocol) but you can write a complex implementation of your ==() function that uses more complex logic to decide how to compare two objects. You could even add a class variable to your class and have your ==() function use that variable in deciding how to compare values, although that seems like bad design.






              share|improve this answer
























                up vote
                1
                down vote













                You can't have multiple definitions of the ==() function (the function that gives you conformance to the equatable protocol) but you can write a complex implementation of your ==() function that uses more complex logic to decide how to compare two objects. You could even add a class variable to your class and have your ==() function use that variable in deciding how to compare values, although that seems like bad design.






                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You can't have multiple definitions of the ==() function (the function that gives you conformance to the equatable protocol) but you can write a complex implementation of your ==() function that uses more complex logic to decide how to compare two objects. You could even add a class variable to your class and have your ==() function use that variable in deciding how to compare values, although that seems like bad design.






                  share|improve this answer












                  You can't have multiple definitions of the ==() function (the function that gives you conformance to the equatable protocol) but you can write a complex implementation of your ==() function that uses more complex logic to decide how to compare two objects. You could even add a class variable to your class and have your ==() function use that variable in deciding how to compare values, although that seems like bad design.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 14:56









                  Duncan C

                  91.4k13114194




                  91.4k13114194



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249828%2fmultiple-equatable-functions-in-one-class%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







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      ReactJS Fetched API data displays live - need Data displayed static

                      政党