views:

377

answers:

5
+9  Q: 

Scala Unit type

I use opencsv to parse csv files, and my code is

while( (line = reader.readNext()) != null ) { .... }

I got a compiler warning saying:

 comparing values of types Unit and Null using `!=' will always yield true
 [warn]     while( (aLine = reader.readNext()) != null ) {

How should I do the while loop?

+4  A: 

Assignment in Scala doesn't return a value, Unit is similar to void in C or C++.

try

var line = ""

while ({line = reader.readNext(); line != null}) { ... }

This works because the value of the last expression in a block is returned and in this case it is a Boolean which is required by the while

Don Mackenzie
`Unit` in Scala is more similar to `void` than to `null`.
Jesper
Good point thanks, I'll correct the above.
Don Mackenzie
+6  A: 

In your case (line = reader.readNext()) is a functional literal that returns type Unit. You may rewrite the code as follows:

while( {line = reader.readNext();  line!= null} ) { .... }
Vasil Remeniuk
Please do not accept this answer: whilst true it is not idiomatic Scala and there are much better ways of achieving what you want (see my answer below)
oxbow_lakes
@oxbow_lakes, perhaps you are being a bit stern here? The while keyword is used hundreds of times in the 2.8 standard library by the core team and this answer is performant, concise and most importantly, looks like my answer too ;@))
Don Mackenzie
I stand by the comment, I'm afraid. This is not idiomatic. It's not even more performance than the *functional* answers because your code will get compiled to a function (i.e. object) instantiation
oxbow_lakes
+9  A: 

An assignment expression has type Unit in Scala. That is the reason for your compiler warning.

There is a nice idiom in Scala that avoids the while loop:

val iterator = Iterator.continually(reader.readNext()).takeWhile(_ != null)

This gives you an iterator over whatever reader.readNext returns.

The continually method returns an "infinite" iterator and takeWhile takes the prefix of that, up to but not including the first null.

(Scala 2.8)

mkneissl
Hmm. My answer does not really match the question. I'll keep it, it might be useful anyway.
mkneissl
@mkneissl Why don't you think your answer matches the question?
Ken Bloom
@Ken: portoalet asked for an explanation for the warning about Unit. I answered with a loop idiom. I've just added the explanation for the warning to make the answer complete.
mkneissl
@mkneissl: thanks for clarifying. I was trying to figure out what it was about your code that *didn't* do the same thing as his while loop. Since it does the same thing, the reason your answer didn't match the question is because it didn't talk about the warning.
Ken Bloom
+5  A: 

You are writing Scala code they way you would write it in Java. Try doing it in a more Scala-like way. To read a text file line by line and do something with each line, try this:

import java.io.File
import scala.io.Source

Source.fromFile(new File("myfile.txt")).getLines.foreach { line =>
    // Do something with the line, for example print it
    println(line)
}
Jesper
He's not doing plain file IO. He's doing CSV IO.
Ken Bloom
@Ken CSV files are normally line-oriented text files, and his code snippet above is about reading a text file line by line. My answer indeed isn't a direct answer to his question, I just wanted to show a way that fits more with the Scala idiom; his original code is Java idiom in Scala.
Jesper
+3  A: 

You can use a Stream to get what you want:

Stream.continually(reader.readLine()).takeWhile( _ ne null) foreach { line =>
  //do Stuff
}

This has the added advantage of other cool stuff as well:

Stream.continually(reader.readLine()).takeWhile( _ ne null) match {
  case head #:: tail => //perhaps you need to do stuff with the first element?
  case _             => //empty
}

EDIT - thanks to mkneissl for pointing out I should have included this warning:

scala> Stream.continually(1).take(100000000).foreach(x=>()) 

scala> val s = Stream.continually(1).take(100000000) 
s: scala.collection.immutable.Stream[Int] = Stream(1, ?) 

scala> s.foreach(x=>()) java.lang.OutOfMemoryError: Java heap space
oxbow_lakes
You might want to add a warning not to keep the head of the Stream to avoid running out of memory. Have been caught by this recently when reading huge files.scala> Stream.continually(1).take(100000000).foreach(x=>())is ok, butscala> val s = Stream.continually(1).take(100000000)s: scala.collection.immutable.Stream[Int] = Stream(1, ?)scala> s.foreach(x=>())java.lang.OutOfMemoryError: Java heap space[ bah, no formatting in comments, see pastie: http://paste.pocoo.org/show/226682/ ]
mkneissl
Unless you actually need a Stream, you might as well use Iterator.continually instead of Stream.continually, then you have no worries about problems with Stream.
Seth Tisue
Right Seth, see my answer to this question: http://stackoverflow.com/questions/3062804/scala-unit-type/3063092#3063092 and for a comparison of Stream vs. Iterator see http://stackoverflow.com/questions/1527962/difference-between-iterator-and-stream-in-scala .
mkneissl