The following was produced while following the Scala for Java Programmers tutorial.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
Dr-Schlegels-MacBook-Pro-2:scala danielschlegel$ scala Welcome to Scala 2.12.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_73). Type in expressions for evaluation. Or try :help. scala> object HelloWorld { | def main(args: Array[String]) { | println("Hello, world!") | } | } defined object HelloWorld scala> HelloWorld.main(new Array[String] (0)) Hello, world! scala> import java.util.{Date, Locale} import java.util.{Date, Locale} scala> import java.text.DateFormat import java.text.DateFormat scala> import java.text.DateFormat._ import java.text.DateFormat._ scala> scala> object FrenchDate { | def main(args: Array[String]) { | val now = new Date | val df = getDateInstance(LONG, Locale.FRANCE) | println(df format now) | } | } defined object FrenchDate scala> FrenchDate.main(new Array[String] (0)) 10 octobre 2017 scala> :type FrenchDate FrenchDate.type scala> :type FrenchDate.main _ Array[String] => Unit scala> :type FrenchDate.main <console>:18: error: missing argument list for method main in object FrenchDate Unapplied methods are only converted to functions when a function type is expected. You can make this conversion explicit by writing `main _` or `main(_)` instead of `main`. FrenchDate.main ^ scala> :type new Date java.util.Date scala> 1+x <console>:17: error: not found: value x 1+x ^ scala> object Timer { | def oncePerSecond(callback: () => Unit) { | while (true) { callback(); Thread sleep 1000 } | } | def timeFlies() { | println("time flies like an arrow...") | } | def main(args: Array[String]) { | oncePerSecond(timeFlies) | } | } <console>:24: warning: Eta-expansion of zero-argument method values is deprecated. Did you intend to write Timer.this.timeFlies()? oncePerSecond(timeFlies) ^ defined object Timer scala> Timer.main(new Array[String] (0)) time flies like an arrow... time flies like an arrow... time flies like an arrow... time flies like an arrow... time flies like an arrow... time flies like an arrow... time flies like an arrow... Dr-Schlegels-MacBook-Pro-2:scala danielschlegel$ scala Welcome to Scala 2.12.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_73). Type in expressions for evaluation. Or try :help. scala> class Complex(real: Double, imaginary: Double) { | def re() = real | def im() = imaginary | } defined class Complex scala> def test = new Complex(1.1, 2.2) test: Complex scala> :type test Complex scala> test.re() res0: Double = 1.1 scala> test.im() res1: Double = 2.2 scala> abstract class Tree defined class Tree scala> case class Sum(l: Tree, r: Tree) extends Tree defined class Sum scala> case class Var(n: String) extends Tree defined class Var scala> case class Const(v: Int) extends Tree defined class Const scala> Const(1) res2: Const = Const(1) scala> Const(1).v res3: Int = 1 scala> Sum(Var("x"),Const(1)) res4: Sum = Sum(Var(x),Const(1)) scala> type Environment = String => Int defined type alias Environment scala> def eval(t: Tree, env: Environment): Int = t match { | case Sum(l, r) => eval(l, env) + eval(r, env) | case Var(n) => env(n) | case Const(v) => v | } eval: (t: Tree, env: Environment)Int scala> eval(Sum(Var("x"),Const(1)), { case "x" => 5 }) res5: Int = 6 |