What does the protocol for fuzzy equality look like?
Here is my tinkering with it for reference types.
enum Fuzzy {
case yes, no, maybe (probability: Double)
protocol Equatable: AnyObject {
func isEqual (to: AnyObject) -> Fuzzy
}
}
@main
enum Test {
static func main () async {
let u = A ()
print (u.isEqual (to: u)) // yes
print (u.isEqual (to: A ())) // yes
print (u.isEqual (to: B ())) // no
print (u.isEqual (to: B (v: u.u))) // maybe
let v = u
print (v.isEqual (to: u)) // yes
print (u.isEqual (to: v)) // yes
class C {
}
print (u.isEqual (to: C ())) // no
}
}
enum Fuzzy {
case yes, no, maybe (probability: Double)
protocol Equatable: AnyObject {
func isEqual (to: AnyObject ) -> Fuzzy
}
}
class U: Fuzzy.Equatable {
let u: Int
init (u: Int) {
self.u = u
}
func isEqual (to it: AnyObject) -> Fuzzy {
guard let it = it as? U else {
return .no
}
if it === self {
return .yes
}
else {
return is_equal (to: it)
}
}
// Subclasses to override
func is_equal (to it: U) -> Fuzzy {
.no
}
}
class A: U {
init (v: Int = 2) {
super.init (u: v)
}
override func is_equal (to it: U) -> Fuzzy {
return self.u == it.u ? (((it as? Self) != nil) ? .yes : .maybe (probability: 0.5)) : .no
}
}
class B: U {
init (v: Int = 3) {
super.init(u: v)
}
override func is_equal (to it: U) -> Fuzzy {
return self.u == it.u ? ((it as? Self) != nil ? .yes : .maybe (probability: 0)) : .no
}
}
How would you write this protocol for all nominal types (reference types and value types)?