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?
swift
add a comment |
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?
swift
Optimal solution? The optional solution is to determine what uniquely identifies an object and use that when implementingEquatable
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
add a comment |
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?
swift
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
swift
edited Nov 11 at 18:10
rmaddy
237k27308374
237k27308374
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 implementingEquatable
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
add a comment |
Optimal solution? The optional solution is to determine what uniquely identifies an object and use that when implementingEquatable
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 11 at 14:57
answered Nov 11 at 14:50
fewlinesofcode
1,928515
1,928515
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 11 at 14:56
Duncan C
91.5k13114194
91.5k13114194
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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