1

Is there anyway we can read scala doc comments using reflection. My requirement is to read the @group tag value and use it for counting how many functions are there for each group

3
  • 2
    Why do you need reflection? Why not just work with source as a text file? Commented Jun 14, 2019 at 14:35
  • ok, i thought using reflection makes it easy. I will try with text file read Commented Jun 14, 2019 at 14:38
  • 3
    This compiler plugin might be useful: takezoe/runtime-scaladoc-reader. Commented Jun 14, 2019 at 15:03

1 Answer 1

4

No, you can't use Scala reflection to access documentation comments. The reason is simple: comments are, almost by definition, not part of the program. Therefore, it is logically impossible for them to be available via reflection.

In Python, for example, documentation is available from the running program (in fact, even without using reflection), because the documentation is not hidden away in comments, but rather simply assigned to a field of the object that is being documented. Many Lisps (e.g. Clojure), and also Ioke and Seph work that way, too.

In Newspeak, what they call "comments" is available using reflection, but that's because what they call "comments" are not really comments, it is more like arbitrary metadata that can be attached to objects. It is in fact more similar to an annotation in Scala than a comment.

In Scala, documentation is written in comments, and comments are not part of the program (they are literally equivalent to whitespace in the Scala Language Specification), and therefore, cannot possibly be part of the program and thus cannot possibly be accessed via reflection.

Sign up to request clarification or add additional context in comments.

2 Comments

Comments are different from whitespace in Scala because of the weird NL token rule scala-lang.org/files/archive/spec/2.13/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.