Skip to content

Commit e7c5415

Browse files
committed
add code tabs.
1 parent 78314ae commit e7c5415

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

_overviews/scala3-book/methods-main-methods.md

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ next-page: methods-summary
1111

1212
Scala 3 offers a new way to define programs that can be invoked from the command line: Adding a `@main` annotation to a method turns it into entry point of an executable program:
1313

14+
{% tabs method_1 %}
15+
{% tab 'Scala 3 Only' for=method_1 %}
16+
1417
```scala
1518
@main def hello() = println("Hello, world")
1619
```
1720

21+
{% endtab %}
22+
{% endtabs %}
23+
1824
Just save that line of code in a file named something like *Hello.scala*---the filename doesn’t have to match the method name---and run it with `scala`:
1925

2026
```bash
@@ -36,6 +42,9 @@ Learn more about the `@main` annotation by reading the following sections, or by
3642
With this approach your `@main` method can handle command line arguments, and those arguments can have different types.
3743
For example, given this `@main` method that takes an `Int`, a `String`, and a varargs `String*` parameter:
3844

45+
{% tabs method_2 %}
46+
{% tab 'Scala 3 Only' for=method_2 %}
47+
3948
```scala
4049
@main def happyBirthday(age: Int, name: String, others: String*) =
4150
val suffix = (age % 100) match
@@ -51,6 +60,9 @@ For example, given this `@main` method that takes an `Int`, a `String`, and a va
5160
println(sb.toString)
5261
```
5362

63+
{% endtab %}
64+
{% endtabs %}
65+
5466
When you compile that code, it creates a main program named `happyBirthday` that’s called like this:
5567

5668
```
@@ -73,8 +85,6 @@ $ scala happyBirthday sixty Fred
7385
Illegal command line: java.lang.NumberFormatException: For input string: "sixty"
7486
```
7587

76-
77-
7888
## The details
7989

8090
The Scala compiler generates a program from an `@main` method `f` as follows:
@@ -85,6 +95,9 @@ The Scala compiler generates a program from an `@main` method `f` as follows:
8595

8696
For instance, the `happyBirthday` method above generates additional code equivalent to the following class:
8797

98+
{% tabs method_3 %}
99+
{% tab 'Scala 3 Only' for=method_3 %}
100+
88101
```scala
89102
final class happyBirthday {
90103
import scala.util.{CommandLineParser as CLP}
@@ -104,12 +117,13 @@ final class happyBirthday {
104117
> This feature is not available for user programs in Scala.
105118
> Regular “static” members are generated in Scala using objects instead.
106119
107-
120+
{% endtab %}
121+
{% endtabs %}
108122

109123
## Scala 3 compared to Scala 2
110124

111-
`@main` methods are the recommended way to generate programs that can be invoked from the command line in Scala 3.
112-
They replace the previous approach in Scala 2, which was to create an `object` that extends the `App` class:
125+
{% tabs method_4 class=tabs-scala-version %}
126+
{% tab 'Scala 2' for=method_4 %}
113127

114128
```scala
115129
// scala 2
@@ -118,16 +132,26 @@ object happyBirthday extends App {
118132
}
119133
```
120134

121-
The previous functionality of `App`, which relied on the “magic” `DelayedInit` trait, is no longer available.
122-
`App` still exists in limited form for now, but it doesn’t support command line arguments and will be deprecated in the future.
135+
{% endtab %}
123136

124-
If programs need to cross-build between Scala 2 and Scala 3, it’s recommended to use an explicit `main` method with an `Array[String]` argument instead:
137+
{% tab 'Scala 3' for=method_4 %}
125138

126139
```scala
127140
object happyBirthday:
128141
def main(args: Array[String]) = println("Hello, world")
129142
```
130143

144+
> `@main` methods are the recommended way to generate programs that can be invoked from the command line in Scala 3.
145+
> They replace the previous approach in Scala 2, which was to create an `object` that extends the `App` class:
146+
147+
> The previous functionality of `App`, which relied on the “magic” `DelayedInit` trait, is no longer available.
148+
> `App` still exists in limited form for now, but it doesn’t support command line arguments and will be deprecated in the future.
149+
150+
> If programs need to cross-build between Scala 2 and Scala 3, it’s recommended to use an explicit `main` method with an `Array[String]` argument instead:
151+
152+
{% endtab %}
153+
{% endtabs %}
154+
131155
If you place that code in a file named *happyBirthday.scala*, you can then compile it with `scalac` and run it with `scala`, as shown previously:
132156

133157
```bash

0 commit comments

Comments
 (0)