[proposal] default func ==(_:_:)

Feature name - provide default func ==(_:_:) for types declared Equatable

Author: Jan E.Schotsman

Introduction:

If all of a type's stored properties are Equatable it makes sense for Swift to provide a default equality implementation.

Motivation:

In a project where many large types are defined that need to be Equatable it gets cumbersome to write explicit equality functions for all types.

Proposed solution

If a type is declared Equatable, all its stored properties are Equatable and no func ==(_:_:) is defined by the developer the compiler will synthetisize a memberwise equality function.
(Recursiveness of this procedure is almost automatic)

Impact on existing code

There is no impact on existing code. Equality functions equal to the default implementation can simply be removed.

I’d expand that to allow any protocol to be annotated “@memberwise” or something, if their requirements can be satisfied in that manner.

Have we already talked about this? I can’t remember.

- Dave Sweeris

···

On Jan 27, 2016, at 00:40, Jan E. Schotsman via swift-evolution <swift-evolution@swift.org> wrote:

Feature name - provide default func ==(_:_:) for types declared Equatable

Author: Jan E.Schotsman

Introduction:

If all of a type's stored properties are Equatable it makes sense for Swift to provide a default equality implementation.

Motivation:

In a project where many large types are defined that need to be Equatable it gets cumbersome to write explicit equality functions for all types.

Proposed solution

If a type is declared Equatable, all its stored properties are Equatable and no func ==(_:_:) is defined by the developer the compiler will synthetisize a memberwise equality function.
(Recursiveness of this procedure is almost automatic)

Impact on existing code

There is no impact on existing code. Equality functions equal to the default implementation can simply be removed.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

I like this idea.

Just a few thoughts:

Will we need some mechanism to mark properties that shouldn’t be compared?
What about computed properties, could we mark these such that they will be compared as well if relevant?
Could I still use this even if some of my properties aren’t equatable? e.g- if nine of ten properties are equatable, could I use this protocol to generate default behaviour for the nine equatable properties, then just add support for the non-equatable one?

If it will require compiler magic anyway (since we currently have no reflection API I assume it will, unless I’ve missed one), then adding support for some or all of these could make this the standard method of implementing equatable for the vast majority of classes.

···

On 27 Jan 2016, at 09:14, Dave via swift-evolution <swift-evolution@swift.org> wrote:

I’d expand that to allow any protocol to be annotated “@memberwise” or something, if their requirements can be satisfied in that manner.

Have we already talked about this? I can’t remember.

- Dave Sweeris

On Jan 27, 2016, at 00:40, Jan E. Schotsman via swift-evolution <swift-evolution@swift.org> wrote:

Feature name - provide default func ==(_:_:) for types declared Equatable

Author: Jan E.Schotsman

Introduction:

If all of a type's stored properties are Equatable it makes sense for Swift to provide a default equality implementation.

Motivation:

In a project where many large types are defined that need to be Equatable it gets cumbersome to write explicit equality functions for all types.

Proposed solution

If a type is declared Equatable, all its stored properties are Equatable and no func ==(_:_:) is defined by the developer the compiler will synthetisize a memberwise equality function.
(Recursiveness of this procedure is almost automatic)

Impact on existing code

There is no impact on existing code. Equality functions equal to the default implementation can simply be removed.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution