Raj
Raj

Reputation: 2398

Compilation Error : 'value foreach is not a member of Unit'

Hi Given below is the code. The function Split_line returns an Array. I suppose the value splitted should be of type Array. But is considered as Unit by the Compiler. What am I doing wrong here?

object Main {

  def Split_line(line: String){
    line.split("\\|\\|")    
  }

  def main(args: Array[String]) {
    val splitted = Split_line("This is a line || hi ")
    //***I am getting error here : 'value foreach is not a member of Unit'
    splitted.foreach(println) 
  }

}

Upvotes: 0

Views: 3999

Answers (1)

Daniel Langdon
Daniel Langdon

Reputation: 5999

That syntax always denotes a function returning unit. Use def Split_line(line: String) = { (with an equals) or better yet, if you are unsure, declare the return type explicitly: def Split_line(line: String): Seq[String] = {

DO note that there are even plans to remove that syntax altogether at some point: "Procedure syntax is dropped in favor of always defining functions with =".

Upvotes: 1

Related Questions