Every code snippet in the reference docs should compile standalone "within reason" #16832
ckipp01
started this conversation in
General Discussion
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've been reading through some parts of the reference docs more thoroughly lately since at work we've been doing a Scala 3 series on new features and one thing that has continually come up both as I'm going through the docs and also as I'm helping newcomers to Scala 3 (and sometimes to Scala in general) go through the docs is that there is a ton of implied knowledge in the code examples, especially since the majority of them aren't actually runnable or able to be ran standalone. I strongly believe that pretty much every code snippet in the docs should be able to be ran, or at the very least pasted and ran. I'll give a couple examples of cases that may seem completely obvious to seasoned Scala devs that are used to Scala 3, but could totally trip up newcomers.
Type Class Derivation
The very top example shown here using
Tree
doesn't compile, because there is noEq, Ordering, Show
. Recently on a workshop we did users assumed they could copy these snippets and get them to work. This is the snippet in question.I think we should be more careful introducing topics without having actual running code or compilable snippets to help better concrete the ideas as people play around with them.
Multiversal Equality
Here we introduce the the idea of why
Universal equality
is dangerous, but we don't actually show a valid code snippet. The user has to infer whatx
is and whaty
is. Simple examples like this could be changed to actually show what we mean. For example here are we talking about?Or are we talking about
These example are actually showing something different, but how is a newcomer supposed to really know the difference?
I could continue to find examples of this on almost every page of the reference documentation. Even when we are talking about simple things like significant indentation, the error message we mention don't actually match the compiler is outputing, so if we try them locally, we don't get the same thing. This can be really confusing to newcomers. Take this small example from indentation rules:
What is the code snippet supposed to actually be showing? The writing seems to make you think the example is supposed to illustrate how the compiler will warn you that something is too far left, but if you run this snippet you get:
Well maybe that's because it's really a script and not a Scala file (fair enough):
Now we finally see the warning (not an error) and instead we see a much bigger error about the
x
not being found which takes the focus away from what the example is supposed to illustrating.As a rule of thumb, I think in this day and age with the tools we have available to us, there is no reason not to have either code snippets that are runnable or code snippets that have been typechecked using something like mdoc.
Beta Was this translation helpful? Give feedback.
All reactions