Scala type mismatch using multiple generic -


i'm trying use scala prototype functionality of lazy endless list lambda calculus classes. public constructor takes 2 arguments , should create lazylist[a,a].

class lazylist[a,b] private (private val first: a, private val mapper: => b, private val successor: => a) {   def this(first: a, successor: => a) = this(first, (e: a) => e, successor)    def head: b = mapper(first)    def tail(): lazylist[a,b] = new lazylist(successor(first), mapper, successor)   def map[r](func: b => r) = new lazylist[a, r](first, func.compose(mapper), successor)   def at(index: long): b = if (index == 0l) head else tail().at(index - 1)   def sublist(end: long): list[b] = if (end == 0l) list(head) else head :: tail().sublist(end - 1)    override def tostring = s"lazylist($first, $mapper, $successor)" } 

but code compilation fails error.

error:(20, 65) type mismatch;  found   : e.type (with underlying type a)  required: b   def this(first: a, successor: => a) = this(first, (e: a) => e, successor)                                                             ^ 

what doing wrong?

parameterized signatures inside class don't have info relation of type b type a, , compiler thinks everywhere inside lazylist's body b not a, that's why compiler complains when try assign a => a a => b.

you should create alternative constructor not this(), factory method in companion object. note a of usage parameter not in way related a inside lazylist's body:

object lazylist {   def apply[a](first: a, successor: => a): lazylist[a, a] = new lazylist(first, identity, successor)  } 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -