Improving presentation of types and signatures in the docs #3493
Replies: 1 comment 3 replies
-
Hello! Thanks for waiting, catching up after my holiday still. This is a fantastic write up and I think you're spot on with your reasoning. This would be a huge improvement to the quality of the generated documentation and I'd love to see this implemented. In order to make it easier to implement and review I think it would be worth breaking it into chunks rather than attempting to delivery it all in a single pull request which could get large and get stuck in reivew-hell. If you have ideas for how to break this down it would be good to have issues for each of them. Otherwise we can have one issue and the implementer can figure it out. Thanks again! Fantastic work! |
Beta Was this translation helpful? Give feedback.
-
Improving the presentation of type signatures and definitions makes the documentation of every single Gleam library nicer and easier to learn.
Gleams' docs print types and type signatures in multiple places; sometimes, it uses the TypeAst to generate the docs (so the docs will show the types as written in the code), while sometimes, it will instead format the concrete type names the type-checker figured out.
In my opinion, both ways it currently works can lead to confusion while reading the docs:
We've discussed this a few times on Discord, and I foolishly, being young, excited, and ignorant, wrote some code first before actually asking what/if something should be done here. So this is me asking! 😄 I'll give my thoughts on all of these questions, but please don't read this as "I have all the anwers" - those are just my opinions and suggestions, and it is never my intention to come across like that. Basically all of this is very much subjective. I'm looking forward to hearing all of your take on those as well!
How should type names be formatted presented?
In general, I think the types names that are shown should always be
To this end, I believe showing the resolved types from the type checker here is preferable in general to showing the type the library author typed. Types in the same module may be shown unqualified, if they don't collide with a type from the prelude - those are already unique and I believe it's clear from the context that they come from the current module. In other cases, I suggest showing the type the same way a consumer would use them by default, qualified with the last bit of their module path:
I think a nice addition here would be to be able to hover type names to see their fully-qualified name, link to the documentation of this type.
Private types and type aliases
This still means that often, internal types might be shown. A common pattern used in packages is to define types in an internal module, and then use a public type alias in a public module to "re-export" them.
Currently, this is not that big of a problems because of 2 things:
With the changes above, this would lead to signatures like this:
where
vdom
is an internal module, even though the library author used publicly exported type aliases.I think a good ruleset would be:
as
), it gets expanded until a public type is foundShould type variable names be shown?
The names chosen for type variables often help to understand what is what, especially if there are multiple type variables in a signature. In that way, they work similarly in my opinion to the names of function variables, which are already shown and also serve documentation purposes. For example, I think these are easier to read than the latter:
If no type annotation is present, the name of the type variable from the type definition side might be used.
If a private type alias is used using type variables, those names could be "copied over" into their expanded form.
Thanks for reading! 😔
Beta Was this translation helpful? Give feedback.
All reactions