|
| 1 | +/* |
| 2 | + * Scala (https://www.scala-lang.org) |
| 3 | + * |
| 4 | + * Copyright EPFL and Lightbend, Inc. dba Akka |
| 5 | + * |
| 6 | + * Licensed under Apache License 2.0 |
| 7 | + * (http://www.apache.org/licenses/LICENSE-2.0). |
| 8 | + * |
| 9 | + * See the NOTICE file distributed with this work for |
| 10 | + * additional information regarding copyright ownership. |
| 11 | + */ |
| 12 | + |
| 13 | +package scala |
| 14 | + |
| 15 | +import java.lang.System.{currentTimeMillis => currentTime} |
| 16 | + |
| 17 | +import scala.annotation.nowarn |
| 18 | +import scala.collection.mutable.ListBuffer |
| 19 | + |
| 20 | +/** The `App` trait can be used to quickly turn objects |
| 21 | + * into executable programs. Here is an example: |
| 22 | + * {{{ |
| 23 | + * object Main extends App { |
| 24 | + * Console.println("Hello World: " + (args mkString ", ")) |
| 25 | + * } |
| 26 | + * }}} |
| 27 | + * |
| 28 | + * No explicit `main` method is needed. Instead, |
| 29 | + * the whole class body becomes the “main method”. |
| 30 | + * |
| 31 | + * `args` returns the current command line arguments as an array. |
| 32 | + * |
| 33 | + * ==Caveats== |
| 34 | + * |
| 35 | + * '''''It should be noted that this trait is implemented using the [[DelayedInit]] |
| 36 | + * functionality, which means that fields of the object will not have been initialized |
| 37 | + * before the main method has been executed.''''' |
| 38 | + * |
| 39 | + * Future versions of this trait will no longer extend `DelayedInit`. |
| 40 | + * |
| 41 | + * In Scala 3, the `DelayedInit` feature was dropped. `App` exists only in a limited form |
| 42 | + * that also does not support command line arguments and will be deprecated in the future. |
| 43 | + * |
| 44 | + * [[https://docs.scala-lang.org/scala3/book/methods-main-methods.html @main]] methods are the |
| 45 | + * recommended scheme to generate programs that can be invoked from the command line in Scala 3. |
| 46 | + * |
| 47 | + * {{{ |
| 48 | + * @main def runMyProgram(args: String*): Unit = { |
| 49 | + * // your program here |
| 50 | + * } |
| 51 | + * }}} |
| 52 | + * |
| 53 | + * If programs need to cross-build between Scala 2 and Scala 3, it is recommended to use an |
| 54 | + * explicit `main` method: |
| 55 | + * {{{ |
| 56 | + * object Main { |
| 57 | + * def main(args: Array[String]): Unit = { |
| 58 | + * // your program here |
| 59 | + * } |
| 60 | + * } |
| 61 | + * }}} |
| 62 | + */ |
| 63 | +@nowarn("""cat=deprecation&origin=scala\.DelayedInit""") |
| 64 | +trait App extends DelayedInit { |
| 65 | + |
| 66 | + /** The time when the execution of this program started, in milliseconds since 1 |
| 67 | + * January 1970 UTC. */ |
| 68 | + final val executionStart: Long = currentTime |
| 69 | + |
| 70 | + /** The command line arguments passed to the application's `main` method. |
| 71 | + */ |
| 72 | + protected final def args: Array[String] = _args |
| 73 | + |
| 74 | + private[this] var _args: Array[String] = _ |
| 75 | + |
| 76 | + private[this] val initCode = new ListBuffer[() => Unit] |
| 77 | + |
| 78 | + /** The init hook. This saves all initialization code for execution within `main`. |
| 79 | + * This method is normally never called directly from user code. |
| 80 | + * Instead it is called as compiler-generated code for those classes and objects |
| 81 | + * (but not traits) that inherit from the `DelayedInit` trait and that do not |
| 82 | + * themselves define a `delayedInit` method. |
| 83 | + * @param body the initialization code to be stored for later execution |
| 84 | + */ |
| 85 | + @deprecated("the delayedInit mechanism will disappear", "2.11.0") |
| 86 | + override def delayedInit(body: => Unit): Unit = { |
| 87 | + initCode += (() => body) |
| 88 | + } |
| 89 | + |
| 90 | + /** The main method. |
| 91 | + * This stores all arguments so that they can be retrieved with `args` |
| 92 | + * and then executes all initialization code segments in the order in which |
| 93 | + * they were passed to `delayedInit`. |
| 94 | + * @param args the arguments passed to the main method |
| 95 | + */ |
| 96 | + final def main(args: Array[String]) = { |
| 97 | + this._args = args |
| 98 | + for (proc <- initCode) proc() |
| 99 | + if (util.Properties.propIsSet("scala.time")) { |
| 100 | + val total = currentTime - executionStart |
| 101 | + Console.println("[total " + total + "ms]") |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments