Make42
Make42

Reputation: 13108

How to have undefined inputs and outputs?

The code

def partval(partID: Int, iter: Iterator[T >: Nothing]): Iterator[T >: Nothing] = {
            iter.map( x => (partID, x) ).toList.iterator }

does not work. The exact type in the Iterator should not matter in this code and I thought that everything should be supertype of Nothing. I thought the Scala compiler could infer the types so I expected even

def partval(partID: Int, iter: Iterator): Iterator = {
            iter.map( x => (partID, x) ).toList.iterator }

or

def partval(partID, iter) = {
            iter.map( x => (partID, x) ).toList.iterator }

to work, but it doesn't. How do I get this to run?


Edit:

The signature def partval(partID: Int, iter: Iterator[T]): Iterator[(Int, T)] results in

Name: Compile Error
Message: <console>:19: error: not found: type T
       def partval2(partID: Int, iter: Iterator[T]): Iterator[(Int, T)] = {
                                                                    ^
<console>:19: error: not found: type T
       def partval2(partID: Int, iter: Iterator[T]): Iterator[(Int, T)] = {

Upvotes: 0

Views: 86

Answers (1)

Vitalii Kotliarenko
Vitalii Kotliarenko

Reputation: 2967

You don't need to specify any type bounds to accept all types:

def partval[T](partID: Int, iter: Iterator[T]): Iterator[(Int, T)] = {
  iter.map(x => (partID, x))
}

Upvotes: 3

Related Questions