I am trying to write a function that counts the number of elements in an array of elements of type T that pass a test of type T=>Boolean.
What I have so far is:
def countPass[T](elem: Array[T]) = {
var count = 0
for(x <- elem)
x match {
case x: T => count += 1
}
count
}
//TEST
println(countPass[Boolean](Array(true, 52, "test")))
I'm getting a couple of errors, the first one is:
combinators.scala:45: warning: abstract type pattern T is unchecked since
it is eliminated by erasure
case x: T => count += 1
^
The next error is:
combinators.scala:54: error: type mismatch;
found : Int(52)
required: Boolean
println(countPass[Boolean](Array(true, 52, "test")))
^
combinators.scala:54: error: type mismatch;
found : String("test")
required: Boolean
println(countPass[Boolean](Array(true, 52, "test")))
^
I'm not sure about whats happening with the first error, but with the second error it throws an exception anytime its not a boolean. I don't want that to happen because I'm just counting the amount of elements of type T are in the array.
Question: How should I refactor my code to fix both of these errors?