In my Silver/Toffee project I’m using a Swift dictionary declared as [A : B]
A is a custom class. I thought implementing the equality operator would be sufficient for value lookups to work but that doesn’t seem to be the case.
public func ==(lhs: A, rhs: A) -> Bool {
return lhs.stringValue == rhs.stringValue
}
public class A {
// ...
var stringValue: String {
// ...
return "someStringRepresentingThisValue"
}
}
Am I approaching this the wrong way? Usually in Swift I would mark class A as Equatable but it seems the IEquatable protocol isn’t quite the same.
Since class A is used in two projects (Island Windows DLL and Toffee shared library) using __partial classes, I was hoping to rely on the custom == operator. I guess I’m missing something for that to happen.
– EDIT –
The custom operator will be called if I use the following code, which makes sense:
Yeah, for some platform-intrinsic stuff like that, you can’t get around some per-platform IFDefs (for now)…
hmm., I’d expect both of the versions you posted to call the custom operator. How is art defined? Could you create a small testcase that shows it only working with the as! cast (which seams wrong, of course — because it’ll throw, if that fails), so that we can have a look and maybe improve/fix this?
Won’t give the result you want, “any” wouldn’t validate for the equals (because if we did that every any == any would fail because ALL overloads would match).
Right now there’s no clean cross-platform way to do this. You will need to implement the proper pattern for each platform (eg GetHash/Equals on .NET, hash/isEqual for Cocoa, etc. See the implementation of Url in RTL2 for an example.
I just looked at the Url implementation in RTL2. I’m running this on Island, however. I was thinking of implementing Equatable but the compiler keeps asking me to implement “static op_Equality(...) -> Bool”. I tried adding this as a class func, as a static func and as a Swift operator (class func ==(...)), to no avail. I also tried just marking the class as Hashable and adding the hashValue property, no go again.
sounds like a bad error message (can you give ma a concrete example so I can log?). the syntax would be
operator Equals(a, b: ByObject): Boolean
(where you can also provide additional overloads to compare your object to others. Url does that, for Cocoa'sNSURL`). We should check with @ck or @EvgenyK to make sure there = operator is actually what Island RTL dictionary uses under the hood — as I myself am not that deeply familiar with the Island RTL API there…
That said, a cleaner way to do tis cross-platform would be nice, Ill log that for investigation.