Skip to content
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

Mark subclasses of Dimension final. #1761

Merged
merged 2 commits into from Nov 13, 2018

Conversation

tkremenek
Copy link
Member

@tkremenek tkremenek commented Nov 13, 2018

Possible resolution of blocking adoption of -swift-version 5:

apple/swift#20374

@tkremenek
Copy link
Member Author

@swift-ci test

@@ -248,7 +248,7 @@ open class Dimension : Unit {
}
}

open class UnitAcceleration : Dimension {
final class UnitAcceleration : Dimension {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we can take this patch? These subclasses are open in ObjC, and we want as-close-as-possible source parity.

Copy link
Contributor

Choose a reason for hiding this comment

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

(I assume we're trying to work around the fact that Swift 5 prevents us from having a pure Swift class that has the equivalent of + (instancetype)baseUnit?)

Copy link
Member Author

Choose a reason for hiding this comment

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

At this point I'm testing the option for consideration. If we took it there are other things to consider, such as parity with Objective-C and expectations for users of the APIs.

Copy link
Contributor

Choose a reason for hiding this comment

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

IIRC, subclassing is part of the contract of this API. (I will check tomorrow.)

I have a patch I can PR that moves .baseUnit() to be of Dimension type everywhere, erasing the type but allowing the build to work. I will discuss it with @parkera tomorrow morning.

Copy link
Member Author

@tkremenek tkremenek Nov 13, 2018

Choose a reason for hiding this comment

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

I suspect the type erasure approach will be possibly source-breaking. Clients using the class method directly on subclasses would get Dimension instead of the more-specific type, and some code may already depend on having a more specific UnitX type as the result of calling baseUnit.

An alternative is to add a runtime check within the baseUnit override implementations to make the type safety provided by a dynamic runtime check. I'm not sure if that is in the contact of the Objective-C version either. Fundamentally, the current baseUnit API isn't type-safe as declared in both Foundation in Objective-C and corelibs-Foundation as a subclass of a subclass of Dimension that does not override baseUnit will be declared as returning Self but won't — it will return an instance of the parent class. That's a type safety hole.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, and Swift is just highlighting it. :(

I'll circle back before EOD.

Copy link
Contributor

Choose a reason for hiding this comment

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

(It feels like Swift could allow Self class methods if they were similar to required initializers, wherein it is not valid to express a class that doesn't override the method.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that sounds like potentially a reasonable language extension that does not exist today.

Copy link
Member

Choose a reason for hiding this comment

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

For now, to unblock Swift 5 adoption, we will take this patch.

@tkremenek
Copy link
Member Author

@swift-ci test

1 similar comment
@tkremenek
Copy link
Member Author

@swift-ci test

@millenomi
Copy link
Contributor

We had a side discussion between me, @tkremenek and @parkera and decided that the right way forward is to require subclasses to be made final.

@millenomi millenomi merged commit 2261c6b into apple:master Nov 13, 2018
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.

None yet

3 participants