Online IDE to run the following scala code

Viewed 196

I want to test the following scala code. I want to use an online editor. I tried scalafiddle and jdoodle but both can't identify the work package. Is there an online IDE where I can run the following code?

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 Answers

In Scastie, try turning Worksheet mode OFF and then extend App to provide main method, for example, try pasting the following in 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)
}

Note that P and Q from your example should be in separate compilations units

Assume the following two definitions of objects named X in packages p and q in separate compilation units.

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

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

which means they should be in separate .scala files. I am not sure it is possible to have multiple files in Scatstie.

Related