2

I have started playing with Scala so my code is a bit Java-ish. Below is my code :

object TypeParam extends App{

  class Param1[T](val elem: T*){
    val elems : List[T] = elem.toList
    def toPrint(): Unit ={
      elems.foreach(print)
    }
  }

  @Override
  def main(args : List[String]): Unit ={
    val x= new Param1[Int](1,2,3,4,6,7)
    x.toPrint()
  }
} 

what I am expecting is to get the elements printed but in realty I am not getting any output.

I know that I can print a list easily through forEach but for learning I am trying to blend things up.

Could anyone explain why I am not getting any output,so in scala do we not have class level variable which can be set ?

2 Answers 2

3

By using below syntax you are not overriding main method from App trait, hence you do not see any output.

@Override
def main(args : List[String]): Unit ={

Use below syntax to override main method:

override def main(args: Array[String]) { 

Note use of override keyword. In Scala, it's a mandatory to use override when overriding a concrete method.

Sign up to request clarification or add additional context in comments.

Comments

2

I think the problem with main method. As you're overriding App trait you can put code, you want to execute, in the body of class itself, instead of creating specific method.

Original main in App looks slightly different:

def main(args : scala.Array[scala.Predef.String]) : scala.Unit

1 Comment

Yeah it got fixed. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.