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

Get protocols that are implemented by the Element type of an array #1177

Open
wvteijlingen opened this issue Jul 4, 2023 · 0 comments
Open
Milestone

Comments

@wvteijlingen
Copy link

wvteijlingen commented Jul 4, 2023

If I have an array of a certain Element type, how can I figure out if that Element type implements a protocol?

For example: Given this input, how can I figure out which one of these generics implement SomeProtocol?

struct Foo: SomeProtocol {}

struct Bar {
  var arrayOfFoos = [Foo]
  var arrayOfStrings = [String]
}

This doesn't seem to work:

{% for type in types.types %}
  {% for variable in type.storedVariables where variable.typeName.isArray %}

    {% if variable.typeName.generic.typeParameters.first.implements.SomeProtocol %}
      // {{variable.name}} implements SomeProtocol
    {% else %}
      // {{variable.name}} does not implement SomeProtocol
    {% endif %}

  {% endfor %}
{% endfor %}

The output I'm looking for:

// arrayOfFoos implements SomeProtocol
// arrayOfStrings does not implement SomeProtocol
@art-divin art-divin added this to the 2.2.0 milestone Mar 29, 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

No branches or pull requests

2 participants