blue-sky
blue-sky

Reputation: 53826

Running code via main method

When I try and run below code in Eclipse 'Run as Scala application' is not being displayed. Is the main method defined correctly ?

 package week4

    class Nil[T] extends List[T] {
      def isEmpty: Boolean = true
      def head: Nothing = throw new NoSuchElementException("Nil.head")
      def tail: Nothing = throw new NoSuchElementException("Nil.tail")
    }

    trait List[T] {
        def isEmpty: Boolean
        def head: T
        def tail: List[T]
    }

    class Cons[T](val head: T, val tail: List[T]) extends List[T]{
      def isEmpty = false
    }

    object List {
      def apply[T](x1: T, x2: T): List[T] = new Cons(x1, new Cons(x2, new Nil))
      def apply[T]() = new Nil

      def main(args:Array[String]) = {
          println(List(1,4))
      }
    }

Upvotes: 0

Views: 218

Answers (2)

idonnie
idonnie

Reputation: 1713

It does not display "Run as Scala application" in a case if .scala file itself resides in a wrong named catalog - it must be named same as last part of a package name (e.g. week4, in our case).

Upvotes: 0

Stephane Landelle
Stephane Landelle

Reputation: 7038

It looks like eclipse/Scala IDE has a hard time find the right object as there's a trait with the same name.

You can for example move your main method into a dedicated object:

object Main {
  def main(args:Array[String]) = {
    println(List(1,4))
  }
}

Upvotes: 1

Related Questions