简体   繁体   中英

How to solve type missmatch in scala

i need some help with this code in scala, i want to implement foldL method but im getting this:

asd.scala:73: error: type mismatch;
 found   : Option[MyTree[A]] => B
 required: B
      def myFoldLeft[B](z: B)(op: (B, A) => B): B = (_:Option[MyTree[A]]) match {
                                                                          ^
one error found

I know thats a type missmatch but im newbie with scala and Oriented Object and i dont understand how to solve this situation.

class MyTree[A](val value: A, val left:Option[MyTree[A]],
                              val right:Option[MyTree[A]]) {

  def myFoldLeft[B](z: B)(op: (B, A) => B): B = (_:Option[MyTree[A]]) match {
    case Some(tree) => right.get.myFoldLeft (left.get.myFoldLeft (op(z, value)) (op)) (op)
    case None => z
  }
}

(_:Option[MyTree[A]])... is a lambda.

You should match like

class MyTree[A](val value: A, val left:Option[MyTree[A]],
                val right:Option[MyTree[A]]) {    
  def myFoldLeft[B](z: B)(op: (B, A) => B): B = (left, right) match {
    case (Some(left), Some(right)) => ???
    case (Some(left), None) => ???
    case (None, Some(right)) => ???
    case (None, None) => ???
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM