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

Add Annotation#types, #vars, #methods #7274

Open
vladfaust opened this issue Jan 5, 2019 · 6 comments
Open

Add Annotation#types, #vars, #methods #7274

vladfaust opened this issue Jan 5, 2019 · 6 comments

Comments

@vladfaust
Copy link
Contributor

These methods would allow to iterate over all types/vars/methods which have the annotation, e.g:

annotation Foo
end

@[Foo]
class Bar
end

{% pp Foo.types %} # => [Bar]

/cc @Blacksmoke16, this could be useful in Athena, right?

I also can find it useful in ORM implementations.

@Blacksmoke16
Copy link
Member

Yes, this would allow me to just get a list of all methods that are tagged as a route action. Currently I am requiring the controller classes to inherit from a predefined class, then iterating over the subclasses of that parent class, then filtering the methods down to just ones with the annotation.

This would be a bit cleaner, and allow the user to define their own inheritance structure.

@vladfaust
Copy link
Contributor Author

vladfaust commented Jan 5, 2019

@Blacksmoke16 there is an alternative possible solution for your case: https://forum.crystal-lang.org/t/type-all-includers/254

@Blacksmoke16
Copy link
Member

Blacksmoke16 commented Apr 17, 2019

@vladfaust What would your use case be for wanting an array of ALL methods/instance vars? Would it make more sense to do something like:

annotation Foo
end

class Klass
  @[Foo]
  def one
    1
  end
  
  @[Foo]
  def two
    2
  end

  def three
    3
  end
end

pp {{Klass.methods(Foo)}} # => [one, two]

Where allow methods/instance_vars on TypeNode to optionally takes an AnnotationType, and only returns methods/ivars with that annotation? This is already currently possible for methods, but would be still not be possible with ivars due to #7504 , would be for sure be cleaner.

@HertzDevil
Copy link
Contributor

If #7648 (comment) works for types, do we still need this feature for instance / class variables and defs, or are there similar workarounds for them?

@Blacksmoke16
Copy link
Member

Blacksmoke16 commented Jun 11, 2021

@HertzDevil You can basically use the same workaround but with @type.methods. Only thing this feature would allow for is like getting an Array(Def) of all methods that have a given annotation irrespective of a specific type. However I'm not sure of how that would be useful/why you would need that.

@kalinon
Copy link
Contributor

kalinon commented Jan 5, 2022

I also realized today that the workaround im used to using (Object.all_subclasses) misses any modules with annotations as well. (unless im missing a way to list modules)

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

No branches or pull requests

5 participants