[swift-evolution] [proposal] default func ==(_:_:)

Jan E. Schotsman jeschot at xs4all.nl
Sun Feb 7 14:36:50 CST 2016


A nice way to enable developers to define an equality function based  
on the default equality operator (available per the current proposal)  
would be:

within the developer's definition of equality for type MyType the  
symbol == means the default equality operator (only for type MyType of  
course)

Then you can do:

func ==(lhs:MyType,rhs:MyTYpe)
  {
  var equal = false
  if lhs == rhs {equal = /*apply some additional test*/}
  return equal
  }

or

func ==(lhs:MyType,rhs:MyType)
  {
  var equal = false
  var lhsCopy = lhs
  var rhsCopy = rhs

  lhsCopy.uncomparedProperty1 = 0
  lhsCopy.uncomparedProperty2 = 0
  rhsCopy.uncomparedProperty1 = 0
  rhsCopy.uncomparedProperty2 = 0

  if lhsCopy == rhsCopy {equal =  
lhs.someMoreRelevantCalculatedProperty ==  
rhs.someMoreRelevantCalculatedProperty}

  return equal
  }



More information about the swift-evolution mailing list