File: S_2.scala

package info (click to toggle)
scala 2.11.12-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 62,828 kB
  • sloc: javascript: 28,808; java: 13,415; xml: 3,250; sh: 1,620; python: 756; makefile: 38; awk: 36; ansic: 6
file content (30 lines) | stat: -rw-r--r-- 971 bytes parent folder | download | duplicates (4)
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
import scala.language.reflectiveCalls
 // Scala class:
class ScalaVarArgs extends J_1 {
  // -- no problem on overriding it using ordinary class
  def method(s: String*) { println(s) }
}

object Test {
  def main(args: Array[String]) {
    //[1] Ok - no problem using inferred type
    val varArgs = new J_1 {
      def method(s: String*) { println(s) }
    }
    varArgs.method("1", "2")

    //[2] Ok -- no problem when explicit set its type after construction
    val b: J_1 = varArgs
    b.method("1", "2")

    //[3] Ok -- no problem on calling its method
    (new ScalaVarArgs).method("1", "2")
    (new ScalaVarArgs: J_1).method("1", "2")

    //[4] Not Ok -- error when assigning anonymous class to an explictly typed val
    // Compiler error:  object creation impossible, since method method in trait VarArgs of type (s: <repeated...>[java.lang.String])Unit is not defined
    val tagged: J_1 = new J_1 {
      def method(s: String*) { println(s) }
    }
  }
}