Reputation: 311
I have a Class defined below:
class NDArray[T](data: List[List[T]])(implicit num: Numeric[T])
.....
I have an object that creates and returns a new NDArray:
object Foo
{
def apply() =
{
new NDArray(List(List())
}
}
I am getting the following error: not enough arguments for constructor NDArray: (implicit num: Numeric[A])com.numscal.matrix.NDArray[A]. Unspecified value parameter num.
I've tried importing Numeric in the object that creates the NDArray, but that doesn't work. My unit tests for NDArray don't import Numeric and they don't have any issues.
I am confused as to what is going on. Any ideas?
Upvotes: 0
Views: 248
Reputation: 5919
data
is a list of lists of type T
. The constructor of NDArray
requires an object of type Numeric[T]
. Because that parameter is declared implicit
, that means that you don't need to specify one explicitly when the compiler can find one in the currently visible scope that also has been defined as implicit
.
Now, since you create an instance of NDArray
without specifying T
, the compiler infers T
. It finds the list of lists, and uses the element type of the inner list as T
. But since you did not specify that one and the list is empty, this defaults to being a List[Nothing]
, therefore, the compiler concludes that T
is the type Nothing
.
Then it searches for an implicit
instance of Numeric[Nothing]
, but that does not exist.
There are several things you can do. Either:
new NDArray(List(List[Int]()))
Or:
new NDArray[Int](List(List()))
(Although I'm not sure if that last one is gonna work. I don't know if the compiler will infer the type parameter of the inner list correctly; you just have to try it.)
The implicit
instance of Numeric[Int]
is already imported by default, since it is part of Predef
. You don't need to import it explicitly. The same goes for all primitive numeric types.
I don't know if you want to use a list of lists of integers, or floats, or whatever. The compiler does not know either, and it can't infer because the list you have given is empty.
Upvotes: 4
Reputation: 457
scala> List(List())
res19: List[List[Nothing]] = List(List())
There is no implicit Numeric for Nothing
, use List.empty[List[TypeYouNeed]]
Upvotes: 2