Skip to content

Conform Never to Error, Equatable, Comparable, and Hashable #16857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

mdiep
Copy link
Contributor

@mdiep mdiep commented May 26, 2018

Implementation for swiftlang/swift-evolution#855.

@@ -28,6 +28,18 @@
@_frozen
public enum Never {}

extension Never: Equatable {
public static func == (lhs: Never, rhs: Never) -> Bool {
switch (lhs, rhs) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cute but I would really rather this be spelled out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure what you mean. 😕

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The switch is required by the compiler right now, no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return true rather than the switch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, ha. Even did that here: https://github.com/pointfreeco/swift-prelude/blob/master/Sources/Prelude/Equatable.swift#L3 I suppose the switch is only required for absurd.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think switch is the better option.

return true is a bit misleading. It's easy to continue thinking that this code could possibly execute. (fatalError("This is impossible. A Never value cannot be constructed") would be better in this regard.)

switch educates the reader about Never and about switch. I think this works as a form of progressive disclosure. You don't need to know that this how it's implemented, but if you go looking, most people are bound to learn something.

I think the swift-evolution discussion bears this out. Several people expressed confusion initially, but walked away with a better understanding.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the goal is pedagogy uber alles, the switch, being needlessly clever, cuts the wrong way. I dont buy the progressive disclosure angle because the most disclosure you get as a consumer of the stdlib is the protocol conformance. As a part of the stdlib, especially as a particularly opaque part of the stdlib, this conformance needs to be optimized for readability and maintenance.

So we’re in an odd position here because anything meant to convey a message at runtime like a fatalError will never happen and is dead weight both literally and semantically. Ultimately, whatever the community settles on, please document this well.

@tkremenek
Copy link
Member

@swift-ci build toolchain

@swift-ci
Copy link
Contributor

swift-ci commented Jun 13, 2018

Linux Toolchain (Ubuntu 16.04)
Download Toolchain
Git Sha - 9a8da862f21340d96ed9e3fbc7f271bbf451c5b6

Install command
tar zxf swift-PR-16857-24-ubuntu16.04.tar.gz
More info

@swift-ci
Copy link
Contributor

swift-ci commented Jun 14, 2018

macOS Toolchain
Download Toolchain
Git Sha - 9a8da862f21340d96ed9e3fbc7f271bbf451c5b6

Install command
tar -zxf swift-PR-16857-31-osx.tar.gz --directory ~/

@shahmishal
Copy link
Member

Updated swift-ci comment with correct download links.

@shahmishal
Copy link
Member

@swift-ci build toolchain

@swift-ci
Copy link
Contributor

Linux Toolchain (Ubuntu 16.04)
Download Toolchain
Git Sha - 9a8da862f21340d96ed9e3fbc7f271bbf451c5b6

Install command
tar zxf swift-PR-16857-34-ubuntu16.04.tar.gz
More info

@swift-ci
Copy link
Contributor

macOS Toolchain
Download Toolchain
Git Sha - 9a8da862f21340d96ed9e3fbc7f271bbf451c5b6

Install command
tar -zxf swift-PR-16857-41-osx.tar.gz --directory ~/

@tkremenek
Copy link
Member

@swift-ci test

@tkremenek
Copy link
Member

@mdiep: This proposal has been accepted. Per the acceptance rationale please also add conformances to Comparable and Error.

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 9a8da862f21340d96ed9e3fbc7f271bbf451c5b6

@mdiep mdiep force-pushed the conform-never-to-equatable-and-hashable branch from 9a8da86 to 4c415b8 Compare July 14, 2018 17:27
@mdiep mdiep changed the title Conform Never to Equatable and Hashable Conform Never to Error, Equatable, Comparable, and Hashable Jul 14, 2018
@mdiep
Copy link
Contributor Author

mdiep commented Jul 17, 2018

How does that look?

@jrose-apple
Copy link
Contributor

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 9a8da862f21340d96ed9e3fbc7f271bbf451c5b6

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 9a8da862f21340d96ed9e3fbc7f271bbf451c5b6

Copy link
Member

@lorentey lorentey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! 👍

@airspeedswift
Copy link
Member

Can we add some form of test? The usual checkEquatable kind of tests won't work because you can't construct one, but forming some code along the lines of the evolution proposal motivation and checking it compiles would be good.

@mdiep mdiep force-pushed the conform-never-to-equatable-and-hashable branch from 4c415b8 to e0dae8e Compare July 18, 2018 00:44
@mdiep
Copy link
Contributor Author

mdiep commented Jul 18, 2018

How does that test look?

@airspeedswift
Copy link
Member

WFM!

@airspeedswift
Copy link
Member

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 4c415b8bbe8e5769d2eb00d4586babc413b6a2ad

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 4c415b8bbe8e5769d2eb00d4586babc413b6a2ad

@mdiep
Copy link
Contributor Author

mdiep commented Jul 18, 2018

CI pick up the wrong SHA for some reason. 😕 It should be testing e0dae8e.

@jrose-apple
Copy link
Contributor

It's a bug with the Jenkins/GitHub integration after a force push. We have it set up to automatically restart and test the correct commit when this happens.

@mdiep
Copy link
Contributor Author

mdiep commented Jul 18, 2018

Can this be merged? 🙂

@airspeedswift airspeedswift merged commit f0cb64d into swiftlang:master Jul 18, 2018
@mdiep mdiep deleted the conform-never-to-equatable-and-hashable branch July 18, 2018 22:52
@mdiep
Copy link
Contributor Author

mdiep commented Jul 18, 2018

Could this be included in 4.2? Or would that pose a potential source compatibility problem?

(I'm happy to open a PR. I just don't want to waste any effort.)

@airspeedswift
Copy link
Member

Probably not at this stage. We’re trying to keep it at just bug fixes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants