1

When I run the below scala program in cloudera quickstart , I getting small error which I unable to rectify it. I am starter for scala programming.

object sort {
  def sort(s:Array[Int] => Unit ){
    var a=new Array[Int](3);
    a(0)=1;
    a(1)=1;
    a(2)=1;
    s(a);
  }

  def qsort(a:Int*){
    println("qsort printed");
  } 

  def main(args:Array[String]){
    sort(qsort);
  }
}

I am getting error

[cloudera@quickstart scalap]$ scalac sort.scala
sort.scala:12: error: type mismatch;
 found   : Seq[Int] => Unit
 required: Array[Int] => Unit
sort(qsort);
     ^
one error found

Please show me the solution.

3
  • sort(qsort(_:_*)) would do the trick. Commented Oct 9, 2015 at 23:26
  • Thanks Kolmar, even this resolved me :) Commented Oct 9, 2015 at 23:32
  • It's more correct to follow Daniel's answer. My comment was more of a joke about cryptic syntax. Commented Oct 9, 2015 at 23:34

1 Answer 1

2

It is simple, you can pass an instance of Array[Int] to a function that expects a Seq[Int], but not vice versa, as Array is more specific than Seq. To fix it, simply change tthe qsort definition to:

def qsort(a:Array[Int]){
  println("qsort printed");
}

or change definition of sort to receive a sequence. Either one will make the types match.

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

2 Comments

Thanks Daniel. Can you some suggestion from where to start to learn scala program . which gives simple way to teach.
Sure. There are plenty of good books out there, or just learn by doing and googling. It kinda depends on if you know other languages or not. Check the coursera free online course from the very authors of scala here.

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.