Las siguientes declaraciones Scala están bien:Genéricos de Scala: ¿Int no conforme con Comparable?
trait Base[B <: Base[B,M,ID], M <: Meta[B,M,ID], ID <: Comparable[ID]] {
// ...
}
trait Meta[B <: Base[B,M,ID], M <: Meta[B,M,ID], ID <: Comparable[ID]] extends Ordered[Meta[_,_,_]] {
// ...
}
trait BaseWithID[B <: BaseWithID[B,M,ID], M <: Meta[B,M,ID], ID <: Comparable[ID]] extends Base[B,M,ID] with Ordered[B] {
// ...
}
trait BaseWithIntID[B <: BaseWithIntID[B,M,ID], M <: MetaWithIntID[B,M,ID], ID <: Comparable[ID]] extends BaseWithID[B,M,ID] {
// ...
}
trait MetaWithIntID[B <: BaseWithIntID[B,M,ID], M <: MetaWithIntID[B,M,ID], ID <: Comparable[ID]] extends Meta[B,M,ID] {
// ...
}
Pero los dos siguientes no son:
trait BaseWithIntID[B <: BaseWithIntID[B,M], M <: MetaWithIntID[B,M]] extends BaseWithID[B,M,Int] {
// ...
}
trait MetaWithIntID[B <: BaseWithIntID[B,M], M <: MetaWithIntID[B,M]] extends Meta[B,M,Int] {
// ...
}
La diferencia es que me quita el parámetro de tipo ID en BaseWithIntID y MetaWithIntID, y especificó Int explícitamente en sus respectivos rasgos básicos. Pero esto no se compila, ¿eso significa que Int no es comparable en Scala? Si es así, ¿qué estoy haciendo mal? Intenté Ordered en lugar de Comparable, y no hizo diferencia.
Estoy usando Eclipse, y como de costumbre, los mensajes de error son inútiles:
type arguments [B,M,Int] do not conform to trait BaseWithID's type parameter bounds [B <: BaseWithID[B,M,ID],M <: Meta[B,M,ID],ID <: java.lang.Comparable[ID]]
Sólo dice que algo está mal, pero no el parámetro tipo está mal, y por qué. En cuanto a this question, pensé que podría probar "ID <% comparable [ID]" en su lugar, pero eso no es legal en una declaración de rasgo.
En realidad, esto no funciona bien (con el mismo mensaje de error):
trait TestBase extends BaseWithID[TestBase,TestMeta,Int]
trait TestMeta extends Meta[TestBase,TestMeta,Int]
El problema con 'ID <% Comparable [ID]' es que define automáticamente un parámetro implícito. Como los rasgos no tienen parámetros, simplemente no funcionará. Como una 'clase', sin embargo, podrías hacerlo. –