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
for other <- others do bldr.append(" and ").append(other)
22
+
bldr.toString
26
23
```
27
24
This would generate a main program `happyBirthday` that could be called like this
28
25
```
@@ -59,29 +56,26 @@ The Scala compiler generates a program from a `@main` method `f` as follows:
59
56
60
57
For instance, the `happyBirthDay` method above would generate additional code equivalent to the following class:
61
58
```scala
62
-
finalclasshappyBirthday {
63
-
importscala.util.{CommandLineParser=>CLP}
64
-
<static> defmain(args: Array[String]):Unit=
65
-
try
66
-
happyBirthday(
67
-
CLP.parseArgument[Int](args, 0),
68
-
CLP.parseArgument[String](args, 1),
69
-
CLP.parseRemainingArguments[String](args, 2))
70
-
catch {
71
-
caseerror: CLP.ParseError=>CLP.showError(error)
72
-
}
73
-
}
59
+
finalclasshappyBirthday:
60
+
importscala.util.{CommandLineParser=>CLP}
61
+
<static> defmain(args: Array[String]):Unit=
62
+
try
63
+
happyBirthday(
64
+
CLP.parseArgument[Int](args, 0),
65
+
CLP.parseArgument[String](args, 1),
66
+
CLP.parseRemainingArguments[String](args, 2))
67
+
catch
68
+
caseerror: CLP.ParseError=>CLP.showError(error)
74
69
```
75
70
**Note**: The `<static>` modifier above expresses that the `main` method is generated
76
71
as a static method of class `happyBirthDay`. It is not available for user programs in Scala. Regular "static" members are generated in Scala using objects instead.
77
72
78
73
`@main` methods are the recommended scheme to generate programs that can be invoked from the command line in Scala 3. They replace the previous scheme to write program as objects with a special `App` parent class. In Scala 2, `happyBirthday` could be written also like this:
79
74
80
75
```scala
81
-
objecthappyBirthdayextendsApp {
82
-
// needs by-hand parsing of arguments vector
83
-
...
84
-
}
76
+
objecthappyBirthdayextendsApp:
77
+
// needs by-hand parsing of arguments vector
78
+
...
85
79
```
86
80
87
81
The previous functionality of `App`, which relied on the "magic" `DelayedInit` trait, is no longer available. `App` still exists in limited form for now, but it does not support command line arguments and will be deprecated in the future. If programs need to cross-build
0 commit comments