1
votes

I have a strange behaviour in the eclipse IDE.

I reproduced it with the documentation on package objects

  • I have a file src/main/scala/gardening/fruits/Fruit.scala containing

    package gardening.fruits

    case class Fruit(name:String)

    object apple extends Fruit("Apple")

    object plum extends Fruit("Plum")

  • a file src/main/scala/gardening/fruits/package.scala containing

    package gardening

    package object fruits { val planted = List(apple, plum)
    def showFruit(fruit: Fruit) { println(fruit.name +"s are ") } }

  • a scala worksheet in src/main/scala/fruitws.sc containing

    import gardening.fruits._

    object PrintPlanted { def main(args: Array[String]) { for (fruit: Fruit <- fruits.planted) { showFruit(fruit) } } }

Now :

  • eclipse says "can't find gardening" (and of course fruits and Fruit)
  • if I hit ctrl space after placing a dot after gardening, some autocompletion appears though
  • stranger, one of those completion is called gardeningfruits (so as a child of gardening, with no dots...)

What am I doing wrong that prevents the worksheet to execute properly ?

edit

I think the package object idea is not available in worksheet.

As a separate point, worksheet might mandate some file organisation on disk that scala files themselves escape (aka, having a file in gardening/fruit/fruit.scala and package gardening only in the file). Not sure..

1
Well, I think this is just a bug. I have experienced something similar myself, though I can't remember if it was related to package objects. I suggest using the REPL instead, it is more reliable.Robin Green
good to know, I'll just go on then, and test some other things.nicolas

1 Answers

4
votes

You need to compile .scala files before you can import them into worksheet