Scala ADT Type Mismatch

Mike Slinn

I'm puzzled by the compile error for the line that defines endState. Here is the code:

import java.util.UUID

object Evaluation {
  def default: Evaluation[Unit, Unit] = new Evaluation[Unit, Unit](identity)
}

case class Evaluation[From, +To](evaluate: (From) => To)

object FSMLike {
  val uuidEmpty: UUID = new UUID(0L, 0L)

  val endState: Evaluation[Unit, FSMLike[Nothing]] = new Evaluation(() => FSMEmpty)

  lazy val stop: FSMEntry[Unit, Unit, Nothing] = FSMEntry(uuidEmpty, Evaluation.default, endState)

  def apply[From1, From2, To](
    action: Evaluation[From1, Unit],
    nextState: Evaluation[From2, FSMLike[To]]
  ): (UUID, FSMLike[To]) = {
    val uuid = UUID.randomUUID
    uuid -> FSMEntry(uuid, action, nextState)
  }
}

sealed trait FSMLike[+A]

case object FSMEmpty extends FSMLike[Nothing]

case class FSMEntry[From1, From2, +To](
  id: UUID,
  action: Evaluation[From1, Unit],
  nextState: Evaluation[From2, FSMLike[To]]
) extends FSMLike[To] {
  def transition(arg1: From1, arg2: From2): FSMLike[To] = {
    action.evaluate(arg1)
    nextState.evaluate(arg2)
  }

  override def toString: String = s"^$id^"
}

Here is the error:

Error:(14, 72) type mismatch;
 found   : () => FSMEmpty.type (with underlying type () => FSMEmpty.type)
 required: Unit => FSMLike[Nothing]
  val endState: Evaluation[Unit, FSMLike[Nothing]] = new Evaluation(() => FSMEmpty)
Alexey Romanov

You are trying to pass () => FSMEmpty, a function with no arguments, where a function with one argument of type Unit is expected. Sure, when you use () as an expression, it's the only value of type Unit, but the left-hand side of => isn't an expression.

You should write _ => FSMEmpty instead. { case () => FSMEmpty } would work as well, I think, but there isn't much point to using it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related