In C++ one is allowed to create a class containing 2 methods, both with the same name, differing strictly in “constness”
An example of this would be:
class Foo {
int bar(int a);
int bar(int a) const;
}
Importing these 2 methods into Swift results in an ambiguity issue since both methods in C++ would map to the same method in Swift.
To disambiguate these 2 methods we need to address 2 main things:
- What determines that 2 methods only differ in “constness”
- How should we go about differentiating them when importing into Swift
What determines that 2 methods only differ in “constness”
Since we can easily check if a method is marked const or not, this question really comes down to figuring out when we can say 2 methods are overloads of each other.
This issue seems hard as methods can
- Be templated
- Have same number of arguments but different types
- Same number of arguments & types but be in a different order
- Different return types but same arguments
The simplest approach would be to consider (for the purpose of disambiguating) 2 methods overloads of each other if they are in the same class/struct (record) and have the same name.
One example of a trade-off here would be that the following class:
class Foo {
int add(int a, int b) const;
void add(int a);
}
would have its methods imported as:
func add(_ a: Int32, _ b: Int32) -> Int32
func addMutating(_ a: Int32)
since they both share the same name and one is const while the other is not. Even though in reality this methods are not overloads and could both be imported without modification.
On the other hand
class Foo {
int add(int a, int b) const;
int add(int a) const;
}
would still get imported as:
func add(_ a: Int32, _ b: Int32) -> Int32
func add(_ a: Int32) -> Int32
since both are marked const we know they are not overloads that differ by “constness”.
We could also do a more complicated approach for determining if 2 methods are overloads by taking arguments and return type into account. Something like Hash(FuncName + Arguments + Return)
This would likely still not be 100% correct at determining 2 methods are strict overloads of each other (templates) but would help in the case shown above.
It seems uncommon that 2 methods in the same class have the same name and differ in “constness” but are not overloads of each other. For that reason it maybe worth just deciding off name alone.
On the other hand, as a user, it may seem weird that two methods which are not overloads of each other result in this modified output (one suffixed with “Mutating”). A more consistent approach maybe to append “Mutating” at the end of every method not marked const . This would be unfortunate though.
How should we go about differentiating them when importing into swift
As shown above one approach would be by modify the name of the mutable version to barMutating .
The mapping would then be:
func bar(_ a: Int32) -> Int32
func barMutating(_ a: Int32) -> Int32
Another potential approach would be to prefix the mutable version with mutating giving:
func bar(_ a: Int32) -> Int32
func mutatingBar(_ a: Int32) -> Int32