在线IDE运行以下Scala代码

时间:2019-06-25 13:48:38

标签: scala

我想测试以下scala代码。我想使用在线编辑器。我尝试了scalafiddlejdoodle,但都无法识别作品package。是否可以在在线IDE中运行以下代码?

package P {
  object X { val x = 1; val y = 2 }
}

package Q {
  object X { val x = true; val y = "" }
}


package P {                  // `X' bound by package clause
import Console._             // `println' bound by wildcard import
object A {
  println("L4: "+X)          // `X' refers to `P.X' here
  object B {
    import Q._               // `X' bound by wildcard import
    println("L7: "+X)        // `X' refers to `Q.X' here
    import X._               // `x' and `y' bound by wildcard import
    println("L8: "+x)        // `x' refers to `Q.X.x' here
    object C {
      val x = 3              // `x' bound by local definition
      println("L12: "+x)     // `x' refers to constant `3' here
      { import Q.X._         // `x' and `y' bound by wildcard import
//      println("L14: "+x)   // reference to `x' is ambiguous here
        import X.y           // `y' bound by explicit import
        println("L16: "+y)   // `y' refers to `Q.X.y' here
        { val x = "abc"      // `x' bound by local definition
          import P.X._       // `x' and `y' bound by wildcard import
//        println("L19: "+y) // reference to `y' is ambiguous here
          println("L20: "+x) // `x' refers to string "abc" here
}}}}}}

1 个答案:

答案 0 :(得分:0)

在Scastie中,尝试先关闭Worksheet模式,然后再extend App提供主要方法,例如,尝试在Scastie中粘贴以下内容:

package P {
  object X { val s = "hello"}
}
package P {
  object Y { val s = "world"}
}

object Hello extends App {
  println(P.X.s)
  println(P.Y.s)
}

请注意,example中的PQ应该位于单独的编译单元中

  

假定包p中名为X的对象的以下两个定义   和q以单独的编译单位

package p {   
  object X { val x = 1; val y = 2 }
}

package q {   
  object X { val x = true; val y = false } 
} 

,这意味着它们应该位于单独的.scala文件中。我不确定Scatstie中是否可以有多个文件。