1

EDIT / UPDATE: PROBLEM IS FIXED!

This is kinda strange as iterating over collections in Scala is usually straight forward but I do get a compile error while using an double array from a case class. The error says:

 error: value foreach is not a member of Array[Double] for(d <- data.data_arr) 

okay, here is the case class:

case class StatsData (name: String,
                  timeUnit: TimeUnit,
                  data_arr: Array[Double],
                  min: Double,
                  max: Double){}

And here is the critical point:

  /*Doesn't work */ 
 for(d <- data.data_arr) {         
  println(d) // can't fetch value d here   
  number = new Number(col, row, d)
 }

Strange thing is, no matter what kind of iteration I'm using, it simply doesn't work. For example using array index

 for (i <- data_arr.length-1)

as well as converting the array to a sequence throws exactly the same error as above;

for(d <- data.data_arr.toSeq)

What am I doing wrong?

Thanks for any help on this matter.

EDIT / UPDATE: PROBLEM IS FIXED!

As it turns out, the cause of the problem was an issue within IntelliJ's Project settings or structure, I cannot say it for certain but it was all solved by creating a new project. The same code runs now perfectly fine. Sorry for that but nearly all of the post has helped me to trace the issue down.

@yakshaver

What is the "yield" expression exactly used for?

Thanks for all the help.

Marvin.Hansen
  • 1,436
  • 1
  • 15
  • 29
  • Can't reproduce the issue. Have you done a clean compile? – Kim Stebel Dec 15 '12 at 06:01
  • yes, I did several clean compiles before asking, even restarted InteliJ but the issue remains the same. – Marvin.Hansen Dec 15 '12 at 06:05
  • `foreach` is a member of `ArrayOps` not `Array`—but that's weird that it isn't finding the implicit conversion from `Predef`. Try explicitly importing `scala.Predef.doubleArrayOps` (or calling it directly) and see if that fixes the problem. That should help with the diagnosis... – DaoWen Dec 15 '12 at 06:19
  • That makes it really funny because importing Scala.Predef.doubleArrayOps gives me the following error: error: value doubleArrayOp is not a member of object Predef import scala.Predef.doubleArrayOp – Marvin.Hansen Dec 15 '12 at 06:22
  • You forgot the _s_: `scala.Predef.doubleArrayOps` – DaoWen Dec 15 '12 at 06:28
  • Check if `Array` is shadowed by a type parameter of some sort. Also try to `prinln(data.data_arr)` to see if that looks like an array. – huynhjl Dec 15 '12 at 06:28
  • @huynhjl - Explicitly calling `scala.Predef.doubleArrayOps` should tell us whether or not there's a type mismatch (e.g. if `Array` is being shadowed), which is why I recommended that. – DaoWen Dec 15 '12 at 06:31
  • As it turns out, it wasn't much of a Scala issue but some sort of really strange compiler issue. I've just created a brand new project and the same code compiles just perfectly fine means iterating the array just works as expected. Sorry guys, I cannot really explain why this happens but it looks like the real cause of the issue was changing the scala version from 2.9 to 2.10RC in IntelliJ so its has most likely to do with incorrect project settings, imho. Thanks for all the good comments. – Marvin.Hansen Dec 15 '12 at 06:40
  • I was using IntelliJ and stumbled upon the same issue. Restarted IntelliJ. Worked out. – Dyin Jun 26 '15 at 11:57

1 Answers1

0

Both

Array(1, 2, 3) foreach println

and

for(i <- Array(1, 2, 3)) yield i

and

for(i <- Array(1, 2, 3)) { println(i) }

work nicely in 2.10.0-RC3 for me.

yakshaver
  • 2,472
  • 1
  • 18
  • 21