Skip to content

Add casting methods to protocols #169

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
merged 2 commits into from
Nov 7, 2019
Merged

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Nov 1, 2019

These return the non type-erased types of a node if it conforms to the given protocol and nil otherwise.

let identifierExprSyntax: IdentifierExprSyntax = /* ... */
let node = Syntax(identifierExprSyntax)
node.as(SyntaxProtocol.self) // returns a IdentifierExprSyntax with static type SyntaxProtocol
node.as(ExprSyntaxProtocol.self) // returns a IdentifierExprSyntax with static type ExprSyntaxProtocol?

With these methods returning the non type-erased type we can get rid of _asConcreteType. The new way to get the concrete type of a Syntax node is to call as(SyntaxProtocol.self) which will return a non-type-erased type that conforms to SyntaxProtocol.

@ahoppen ahoppen changed the title Add casting methods to protocols WIP: Add casting methods to protocols Nov 1, 2019
@ahoppen ahoppen force-pushed the as-protocol branch 2 times, most recently from e7bac89 to b58ae97 Compare November 7, 2019 19:04
@ahoppen ahoppen changed the title WIP: Add casting methods to protocols Add casting methods to protocols Nov 7, 2019
@ahoppen
Copy link
Member Author

ahoppen commented Nov 7, 2019

@swift-ci Please test

…ocol

This allows us to use SyntaxProtocol as a return value again and provide
nice functions that check if type-erased note conforms to a protocol.
@ahoppen
Copy link
Member Author

ahoppen commented Nov 7, 2019

swiftlang/swift-stress-tester#109

@swift-ci Please test

@@ -13,7 +13,7 @@
/// A Syntax node represents a tree of nodes with tokens at the leaves.
/// Each node has accessors for its known children, and allows efficient
/// iteration over the children through its `children` property.
public struct Syntax: SyntaxProtocol {
public struct Syntax: SyntaxProtocol, SyntaxHashable {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not clear to me, why is SyntaxHashable a separate protocol than SyntaxProtocol? Is there something that needs to conform to SyntaxProtocol but not SyntaxHashable or vice-versa?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hashable has a Self requirement and if we make SyntaxProtocol conform to Hashable, we cannot use it as a return type anymore.

These return the non type-erased types of a node if it conforms to the
given protocol and nil otherwise.

With these methods returning the non type-erased type we can get rid of
_asConcreteType.
@ahoppen
Copy link
Member Author

ahoppen commented Nov 7, 2019

Damn, had the wrong version of the swift repo checked out and generated outdated .gyb files. Let's try again. On the upside, CI caught it.

swiftlang/swift-stress-tester#109
@swift-ci Please test

@swift-ci
Copy link
Contributor

swift-ci commented Nov 7, 2019

Build failed
Swift Test OS X Platform
Git Sha - 109833b

@ahoppen ahoppen merged commit b15030f into swiftlang:master Nov 7, 2019
@ahoppen ahoppen deleted the as-protocol branch November 7, 2019 23:16
adevress pushed a commit to adevress/swift-syntax that referenced this pull request Jan 14, 2024
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.

3 participants