Pregunta

Tengo este fragmento de código Scala:

def prologList(l: List[ScalaObject], sep: String) =
    "[" + (if (l isEmpty) "" else l.reduceLeft(_ + sep + _)) + "]"

def neighbors(s: State) = prologList(trans(s).toList, ", ")
def labels(s: State) = prologList(labeling(s).toList, ", ")

La siguiente a la última línea compila bien, pero en la última línea me sale el error

  

List[Char] Encontrado, List[ScalaObject] requerida

(labeling tiene el tipo Map[State, Set[Char]].)

Estoy un poco sorprendido, ya que 1) pensé que List[Char] podría ser visto como un subtipo de List[ScalaObject] (a diferencia de Java), y 2) la línea por encima de las compilaciones última línea! (trans tiene tipo Map[State, Set[State]] aunque ...)

La pregunta es obvia, ¿qué estoy haciendo mal, y cómo puedo solucionarlo?

¿Fue útil?

Solución

Char no es un subtipo de ScalaObject.

En la parte superior tiene Any cual un tipo súper de todo. Probablemente se puede sustituir con ScalaObject Any y que debe hacer su compilación de código.

http://www.scala-lang.org/node/128 para un diagrama de tipo de jerarquía.

En el REPL se puede utilizar la función implícita a relaciones escriba solucionar problemas:

scala> implicitly[Char <:< Any]
res0: <:<[Char,Any] = <function1>

scala> implicitly[Char <:< ScalaObject]
<console>:6: error: could not find implicit value for parameter e: <:<[Char,ScalaObject]
       implicitly[Char <:< ScalaObject]
                 ^

scala> implicitly[List[Char] <:< List[Any]]
res2: <:<[List[Char],List[Any]] = <function1>

scala> implicitly[List[Char] <:< List[ScalaObject]]
<console>:6: error: could not find implicit value for parameter e: <:<[List[Char],List[ScalaObject]]
       implicitly[List[Char] <:< List[ScalaObject]]

Editar:? Por cierto, sabe usted de mkString

trans(s).mkString("[", ", ", "]")
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top