diff --git a/_overviews/scala3-book/taste-toplevel-definitions.md b/_overviews/scala3-book/taste-toplevel-definitions.md index 604ee0a0dd..7448af41ed 100644 --- a/_overviews/scala3-book/taste-toplevel-definitions.md +++ b/_overviews/scala3-book/taste-toplevel-definitions.md @@ -12,6 +12,8 @@ next-page: taste-summary In Scala 3, all kinds of definitions can be written at the “top level” of your source code files. For instance, you can create a file named _MyCoolApp.scala_ and put these contents into it: +{% tabs toplevel_1 %} +{% tab 'Scala 3 only' for=toplevel_1 %} ```scala import scala.collection.mutable.ArrayBuffer @@ -37,15 +39,18 @@ type Money = BigDecimal p.toppings += Cheese println("show me the code".capitalizeAllWords) ``` +{% endtab %} +{% endtabs %} As shown, there’s no need to put those definitions inside a `package`, `class`, or other construct. - ## Replaces package objects If you’re familiar with Scala 2, this approach replaces _package objects_. But while being much easier to use, they work similarly: When you place a definition in a package named _foo_, you can then access that definition under all other packages under _foo_, such as within the _foo.bar_ package in this example: +{% tabs toplevel_2 %} +{% tab 'Scala 3 only' for=toplevel_2 %} ```scala package foo { def double(i: Int) = i * 2 @@ -58,9 +63,9 @@ package foo { } } ``` +{% endtab %} +{% endtabs %} Curly braces are used in this example to put an emphasis on the package nesting. The benefit of this approach is that you can place definitions under a package named _com.acme.myapp_, and then those definitions can be referenced within _com.acme.myapp.model_, _com.acme.myapp.controller_, etc. - -