You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _overviews/scala3-book/methods-main-methods.md
+32-8Lines changed: 32 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -11,10 +11,16 @@ next-page: methods-summary
11
11
12
12
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:
13
13
14
+
{% tabs method_1 %}
15
+
{% tab 'Scala 3 Only' for=method_1 %}
16
+
14
17
```scala
15
18
@main defhello() = println("Hello, world")
16
19
```
17
20
21
+
{% endtab %}
22
+
{% endtabs %}
23
+
18
24
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`:
19
25
20
26
```bash
@@ -36,6 +42,9 @@ Learn more about the `@main` annotation by reading the following sections, or by
36
42
With this approach your `@main` method can handle command line arguments, and those arguments can have different types.
37
43
For example, given this `@main` method that takes an `Int`, a `String`, and a varargs `String*` parameter:
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 %}
123
136
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:
> `@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
+
131
155
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:
0 commit comments