Skip to content

How do I take a ContextDescriptor and find if it conforms to a Protocol? #66

Answered by Richard-Gist
Richard-Gist asked this question in Q&A
Discussion options

You must be logged in to vote

Writing one more answer as I finally got to where I needed to be.

let views = types
    .filter { !$0.flags.isGeneric }
    .compactMap { $0 as? StructDescriptor }
    .compactMap { $0.accessor(MetadataRequest(state: .abstract)) }
    .compactMap { $0.metadata as? StructMetadata }
    .filter { $0.conformances.contains { $0.protocol.name == String(describing: View.self) }}
    .compactMap { $0.type }

This has a big caveat that it does not pick up any views that are generic. For my purposes, that's ok for now, but this at least gets me a lot closer to my goals.


UPDATE:
I found that this version is not restricted to Structs and makes finding conformance more flexible:

types
    .filter { !$0.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Richard-Gist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant