tags:

views:

92

answers:

2
import java.util.Random
class Kostka {
    val rand = new Random(System.currentTimeMillis())
    val value: List[Int] = List(rand.nextInt(6+1))
}
object MyRandom {
    def Fill[A](n: Int): List[A] = {
        if (n<=0) Nil
        else {
            var lst = List[A]
            for (i <- 1 to n){
                lst ++= (new Kostka).value
        }   
            return lst
        }
    }   
}
object Gra {
    def main(args: Array[String]): Unit = {
        println("Podaj liczbe kosci\n")
        val kosci: List[Kostka] = MyRandom.Fill[Kostka](10)
        // Policzenie wyniku
        println("Strzelaj ile razem wypadło\n")
        // przyjecie wyniku
        // dopisac ile wypadlo czyli wynik
        println("Wypadlo: ")
        println(kosci.toString)
    }
}

And error:

a.scala:10: error: missing arguments for method apply in object     List;
follow this method with `_' if you want to treat it as a partially   applied function
            var lst = List[A]
                      ^
one error found

When I have:

var lst = List[A]() 

i got that error:

a.scala:12: error: type mismatch;
 found   : List[Any]
 required: List[A]
                lst ++= (new Kostka).value
                ^
one error found
+3  A: 

in your declaration of lst you forgot the parens : lst = List[A]() In fact, List[A](a,b,c) is a synthatic sugar for List[A].apply(a,b,c), that's why the compiler complained about apply's arguments.

Edit: you can use a ListBuffer instead of your List (in the Fill method, and by the way, the name should be fill(cf http://davetron5000.github.com/scala-style/)) . When you finish the work to do on the buffer, you can call toList, which computes on constant time ;) .

Aymen
What are profits if i use ListBuffer instead of List?
matiit
@matiit: `List` is immutable. `ListBuffer` is mutable. Choose the one that best serves your needs.
Randall Schulz
In particular, adding an element to the end of a `List`, like you do with `lst ++= (new Kostka).value`, is expensive, as `lst` needs to be copied. Adding an element to the end of a `List` takes constant time.
Alexey Romanov
+1  A: 

See Aymen's answer for general guidelines. After your update you have the following effect.

Kostka.value has type List[Int]. lst has type List[A]. The result of the append (++) is the least common supertype of List[Int] and List[A] which is List[Any]. But List[Any] is not a subtype of List[A]. That's why you get the type mismatch.

Your Fill method should not be generic in the first place, unless you make Kostka generic, too.

Furthermore, the use of new Kostka combined with the PRNG initialization looks strange as well.

Finally, in Scala 2.8 there is a fill method on the collection companions:

scala> val r = new java.util.Random
r: java.util.Random = java.util.Random@14a616

scala> List.fill(10) {r.nextInt(6+1)}
res4: List[Int] = List(3, 6, 4, 1, 2, 4, 0, 4, 6, 4)

and, unless your dice are 7-sided, you might go for

scala> List.fill(10) {r.nextInt(6) + 1}
res5: List[Int] = List(2, 5, 2, 1, 1, 4, 4, 2, 6, 3)

instead.

mkneissl