@@ -61,6 +61,28 @@ on [docs.scala-lang.org][home].
6161
6262 And a paragraph, with a [link](https://www.scala-lang.org).
6363
64+ Tables of contents will be automatically generated in a sidebar for your document, and syntax highlighting
65+ is provided.
66+
67+ ### Criteria for Docs to be Accepted
68+
69+ The goal of this documentation repository is to be highly curated, rather than the approach by other community-driven
70+ documentation platforms, like wikis. Therefore, to be included on [ docs.scala-lang.org] [ home ] , a document must:
71+
72+ - ** "fit in"** to the repository (_ i.e.,_ it should not be a complete duplicate of another article),
73+ - ** be polished** , i.e. it must be thorough, complete, correct, and organized; written as an article to be understood
74+ by many users.
75+ - ** be maintained** , if the document might require revisions from time to time, be prepared to keep it up to date, or
76+ nominate someone to take ownership.
77+
78+ If you have something you're thinking about contributing, or that you're thinking about writing in order to contribute
79+ -- we'd love to consider it! Please don't hesitate to use GitHub issues and pull requests and the
80+ ` #scala-contributors ` room [ on Discord] ( https://discord.com/invite/scala ) for any questions, concerns,
81+ clarifications, etc.
82+
83+ ## Code blocks
84+
85+ It's common for various kinds of documents to require code examples.
6486You can contribute code in a markdown document by either
6587- in-line by putting backticks around it,
6688- surrounding by triple backticks,
@@ -79,24 +101,93 @@ indented example:
79101 case class Foo(x: Int)
80102~~~
81103
82- Tables of contents will be automatically generated in a sidebar for your document, and syntax highlighting
83- is provided.
104+ ### Scala 2 vs Scala 3
84105
85- ### Criteria for Docs to be Accepted
106+ Sometimes you would like to compare between Scala 2 and Scala 3 in a document, for example in
107+ our [ Hello World] [ hello-world ] chapter of the Scala Book. Here is an example of how you
108+ can generate the same tabs in markdown with the ` tabs ` directive and class ` tabs-scala-version ` :
86109
87- The goal of this documentation repository is to be highly curated, rather than the approach by other community-driven
88- documentation platforms, like wikis. Therefore, to be included on [ docs.scala-lang.org] [ home ] , a document must:
110+ <!-- {% raw %} -->
111+ ~~~ liquid
112+ {% tabs hello-world-demo class=tabs-scala-version %}
89113
90- - ** "fit in"** to the repository (_ i.e.,_ it should not be a complete duplicate of another article),
91- - ** be polished** , i.e. it must be thorough, complete, correct, and organized; written as an article to be understood
92- by many users.
93- - ** be maintained** , if the document might require revisions from time to time, be prepared to keep it up to date, or
94- nominate someone to take ownership.
114+ {% tab 'Scala 2' for=hello-world-demo %}
115+ ```scala
116+ object hello extends App {
117+ println("Hello, World!")
118+ }
119+ ```
120+ {% endtab %}
95121
96- If you have something you're thinking about contributing, or that you're thinking about writing in order to contribute
97- -- we'd love to consider it! Please don't hesitate to use GitHub issues and pull requests and the
98- ` #scala-contributors ` room [ on Discord] ( https://discord.com/invite/scala ) for any questions, concerns,
99- clarifications, etc.
122+ {% tab 'Scala 3' for=hello-world-demo %}
123+ ```scala
124+ @main def hello() = println("Hello, World!")
125+ ```
126+ {% endtab %}
127+
128+ {% endtabs %}
129+ ~~~
130+ <!-- {% endraw %} -->
131+
132+ It is crucial that you use the ` tabs-scala-version ` class to benefit from some cool user interactions:
133+ - all other Scala version tabs on the same page will also switch to current tab, whenever one is changed.
134+ - the tab picked will be remembered accross the site, and when the user returns to the page after some time.
135+
136+ ### Typechecked Examples
137+
138+ The site build process uses [ mdoc] ( https://scalameta.org/mdoc/ ) to typecheck
139+ code snippets in markdown. This is a great way to ensure the code snippets that
140+ you're including typecheck and are valid. Here are a few quick tips to get
141+ started:
142+
143+ First, add ` mdoc ` after ` scala ` when you are creating a
144+ code block. The ` mdoc ` modifier here will make sure that ` mdoc ` runs the code
145+ snippet and ensures that it's valid.
146+
147+ <div class =" language-plaintext highlighter-rouge " >
148+ <div class="highlight">
149+ <pre class="highlight">
150+ <code class="hljs scala">```scala mdoc
151+ <span class =" hljs-keyword " >val</span > a = <span class =" hljs-number " >1</span >
152+ ``` </code></pre></div></div>
153+
154+ If you have a snippet that you expect to fail, you can also account for this by
155+ using `mdoc:fail` for a compile error `mdoc:crash` for a runtime-error.
156+
157+ <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code class="hljs scala">```scala mdoc:fail
158+ <span class="hljs-keyword">val</span> b: <span class="hljs-type">String</span> = <span class="hljs-number">3</span> <span class="hljs-comment">// won't compile</span>
159+ ```</code></pre></div></div>
160+
161+ Keep in mind that a single file is all compiled as a single unit, so you can't
162+ redefine a variable that was defined above in another code snippet. _However_
163+ there are a couple ways to get around this. Firstly, you can use the `mdoc:nest`
164+ modifier with will wrap the snippet in a `scala.Predef.locally{...}`. This will
165+ essentially "hide" the snippet from the others. Another way around this is to
166+ use the `mdoc:reset` modifier, which _resets_ and forgets about everything up
167+ above. Here is an example using the various modifiers.
168+
169+ <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code class="hljs scala">```scala mdoc
170+ <span class="hljs-keyword">import</span> java.time.<span class="hljs-type">Instant</span>
171+
172+ <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">now</span></span>() = <span class="hljs-type">Instant</span>.now()
173+ <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Foo</span> </span>{}
174+ ```</code></pre></div></div>
175+
176+ <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code class="hljs scala">```scala mdoc:nest
177+ <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span>(<span class="hljs-params">a: <span class="hljs-type">Int</span></span>) <span class="hljs-comment">// conflicts with Foo above, but it's nested so it's fine</span></span>
178+ ```</code></pre></div></div>
179+
180+ <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code class="hljs scala">```scala mdoc
181+ <span class="hljs-keyword">val</span> a = <span class="hljs-string">s"The time is <span class="hljs-subst">${now()}</span>"</span> <span class="hljs-comment">// still have access to the now method from above</span>
182+ ```</code></pre></div></div>
183+
184+ <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code class="hljs scala">```scala mdoc:reset
185+ <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span>(<span class="hljs-params">a: <span class="hljs-type">String</span></span>) <span class="hljs-comment">// forget the previous Foo's and start fresh</span></span>
186+ ```</code></pre></div></div>
187+
188+ <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code class="hljs scala">```scala mdoc
189+ <span class="hljs-keyword">val</span> myFoo = <span class="hljs-type">Foo</span>(<span class="hljs-string">"hi"</span>) <span class="hljs-comment">// now we only have access to the last Foo</span>
190+ ```</code></pre></div></div>
100191
101192## Document Templates
102193
@@ -197,64 +288,9 @@ you should use the following format:
197288 |---------|---------|
198289 | content | more |
199290
200- ### Code blocks
201-
202- The site build process uses [ mdoc] ( https://scalameta.org/mdoc/ ) to typecheck
203- code snippets in markdown. This is a great way to ensure the code snippets that
204- you're including typecheck and are valid. Here are a few quick tips to get
205- started:
206-
207- First, add ` mdoc ` after ` scala ` when you are creating a
208- code block. The ` mdoc ` modifier here will make sure that ` mdoc ` runs the code
209- snippet and ensures that it's valid.
210-
211- <div class =" language-plaintext highlighter-rouge " >
212- <div class="highlight">
213- <pre class="highlight">
214- <code class="hljs scala">```scala mdoc
215- <span class =" hljs-keyword " >val</span > a = <span class =" hljs-number " >1</span >
216- ``` </code></pre></div></div>
217-
218- If you have a snippet that you expect to fail, you can also account for this by
219- using `mdoc:fail` for a compile error `mdoc:crash` for a runtime-error.
220-
221- <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code class="hljs scala">```scala mdoc:fail
222- <span class="hljs-keyword">val</span> b: <span class="hljs-type">String</span> = <span class="hljs-number">3</span> <span class="hljs-comment">// won't compile</span>
223- ```</code></pre></div></div>
224-
225- Keep in mind that a single file is all compiled as a single unit, so you can't
226- redefine a variable that was defined above in another code snippet. _However_
227- there are a couple ways to get around this. Firstly, you can use the `mdoc:nest`
228- modifier with will wrap the snippet in a `scala.Predef.locally{...}`. This will
229- essentially "hide" the snippet from the others. Another way around this is to
230- use the `mdoc:reset` modifier, which _resets_ and forgets about everything up
231- above. Here is an example using the various modifiers.
232-
233- <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code class="hljs scala">```scala mdoc
234- <span class="hljs-keyword">import</span> java.time.<span class="hljs-type">Instant</span>
235-
236- <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">now</span></span>() = <span class="hljs-type">Instant</span>.now()
237- <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Foo</span> </span>{}
238- ```</code></pre></div></div>
239-
240- <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code class="hljs scala">```scala mdoc:nest
241- <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span>(<span class="hljs-params">a: <span class="hljs-type">Int</span></span>) <span class="hljs-comment">// conflicts with Foo above, but it's nested so it's fine</span></span>
242- ```</code></pre></div></div>
243-
244- <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code class="hljs scala">```scala mdoc
245- <span class="hljs-keyword">val</span> a = <span class="hljs-string">s"The time is <span class="hljs-subst">${now()}</span>"</span> <span class="hljs-comment">// still have access to the now method from above</span>
246- ```</code></pre></div></div>
247-
248- <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code class="hljs scala">```scala mdoc:reset
249- <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span>(<span class="hljs-params">a: <span class="hljs-type">String</span></span>) <span class="hljs-comment">// forget the previous Foo's and start fresh</span></span>
250- ```</code></pre></div></div>
251-
252- <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code class="hljs scala">```scala mdoc
253- <span class="hljs-keyword">val</span> myFoo = <span class="hljs-type">Foo</span>(<span class="hljs-string">"hi"</span>) <span class="hljs-comment">// now we only have access to the last Foo</span>
254- ```</code></pre></div></div>
255-
256291[collections-overview]: {% link _overviews/collections-2.13/introduction.md %}
257292[why-contribute]: {% link contribute.md %}
258293[home]: {% link index.md %}
259294[overviews-index]: {% link _overviews/index.md %}
260295[scala-3-reference]: {{ site.scala3ref }}
296+ [hello-world]: {% link _overviews/scala3-book/taste-hello-world.md %}
0 commit comments