-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Allow protected methods on API docs #7799
Allow protected methods on API docs #7799
Conversation
Actually useful for me! |
I'm not sure if there has ever been a discussion about this. But I'm actually fine with not showing protected methods in the API docs. Until now I have never encountered a case where this would be useful. Obviously, methods that are not supposed to be shown in the docs can be |
I agree that private methods should not be shown in the public API docs since they are mostly used internally and not exposed to the end user. However protected methods should be shown since the end user could override them in their subclasses to change the behavior of that subclass. My specific use case would be for a logging shard I am working on. As I showed in the screenshot, I have a protected # The formatter to use, defaults to `Crylog::Formatters::LineFormatter`.
setter formatter : Crylog::Formatters::LogFormatter? = nil
# Returns the `Crylog::Formatters::LogFormatter` defined on `self`, or `#default_formatter` if none was set.
def formatter : Crylog::Formatters::LogFormatter
(f = @formatter) ? f : default_formatter
end
# Can be overridden to use a different formatter on a handler.
protected def default_formatter : Crylog::Formatters::LogFormatter
Crylog::Formatters::LineFormatter.new
end I then have a public It makes sense to have it protected as the user should not use it directly, but to just change the behavior of their handler. |
Exactly the same is true for private methods. Why is your |
# This module is part of a third-party shard, it has API docs online
module Foo
# This method must not be called outside including object,
# but it actually has some logic inside, which could be described
# in this very comment. But it's not included into API docs
protected foo
end
end # This object is part of my application
class ActualObject
include Foo
def call
# What does `Foo#foo` do? I could have read it from the shard's API docs online,
# but it's not there, so I must jump into its source code, what a bummer
foo
end
end |
Because I wanted it to be in the middle ground between private and public. I.e. not public so it couldn't be used on its own, but not private so it would show up in the docs (which currently isn't the case). IMO protected implies that it still could be used by an end user, while private implies like "hey this is used internally, override it your own risk". |
What about adding |
@Blacksmoke16 By the same argument, In my opinion it makes sense to consider both An opt-in flag like |
If protected methods are to be shown, I would put them in a separate section so the user of the types is not bothered with them. |
I could go with this. I could see how including ALL protected methods might not be the best idea. This would only document public methods by default, but at least give the ability to document whatever the developer wants if they so choose, which is currently not possible.
What about having sub sections under class/instance method sections that are categorized by type. In the order of public => protected => private. For example: Class Method SummaryPublic.some_public_method Protected.some_protected_method Then of course if there aren't any protected/private those sub sections would just be omitted. |
🏓 |
I'm still missing a convincing use case for publicly documented methods with |
I would not add a I do see value in adding some documentation so inheritors can know what to do, but for that use case, it will be better IMO to have them in a different section as I pointed out before. |
@bcardiff Is that assuming that protected/private methods would get documented by default, but just appear in their own section? The idea around |
A solution is to have |
Yes, that was the purpose behind |
But why not simply make methods relevant to inheritors |
I mean, on the Web UI @Blacksmoke16 , no code to change. Having a different color scheme for private and protected methods may also be a good idea. |
I don’t see a need to add The nodoc protected method will be used to highlight that the hierarchy behavior is expected to not be overridden outside the shard. (But it is required to be protected because of designs). Users of a package will usually do not care about protected method, so I don’t want to clutter the public docs. |
Maybe we can add a flag to the doc generator to include |
This all sounds like much additional complexity. Much more than necessary. Why not just leave everything as it is? |
I agree with @straight-shoota |
I can create an issue to move the discussion there.
I think there is. Whether others agree with me is another matter. The best example would be when dealing with inheritance, where you have abstract parent classes that use protected methods to control some of logic in the child/parent classes without allowing them to be used outside of the class. This change would allow these methods to be documented so people extending from it are aware of them, without having to document it in some readme or in code. |
This pull request has been mentioned on Crystal Forum. There might be relevant details there: https://forum.crystal-lang.org/t/documenting-protected-methods/6765/2 |
The output of the documentation generator is not just used for the API documentation website, but can give semantic information about the types/methods available in a program that can be used for other things (such as IDE capabilities). Limiting this to only public methods prevents the autocomplete of private/protected methods within relevant areas of the code. |
Relevant: #14816 |
I think we're dealing with two different concepts of public/private: Public/Private for Users: Public/Private for Objects: If a method is designed for user interaction, it can have documentation. While hiding private and protected methods by default makes sense, it would be useful to have an option to document them as well when necessary. When we create shards/libs with modules/abstract classes, we want to add private/protected methods and explain to user how those methods can be used. |
Allows protected methods to be displayed on the generated API docs.
Example: