Assigning a variable with class type

I am trying to write a generic iterator that combines a Fibonacci sequence:

def FibIter[T](fst:T , snd:T)(implicit num:Numeric[T]) = new Iterator[T] { var fn1 = fst var fn2 = snd def hasNext = true def next() = { val ret = fn1 fn1 = fn2 fn2 = num.plus(ret,fn2) ret } } 

However, the compiler complains about the first two assignments of variables:

A parameter type in a structural refinement may not be an abstract type defined outside this refinement

Does anyone have an idea how to solve this problem? Thank you very much!

+4
source share
1 answer

It seems you can get around this using a dedicated class

 class FibIter[T](fst:T , snd:T)(implicit num:Numeric[T]) extends Iterator[T] { var fn1 = fst var fn2 = snd def hasNext = true def next() = { val ret = fn1 fn1 = fn2 fn2 = num.plus(ret,fn2) ret } } 
+5
source

Source: https://habr.com/ru/post/1381220/


All Articles