Contravariant binary operations in Scala
-
04-06-2021 - |
Domanda
For a very basic generic model of categories, I'm trying to get the morphism associated with a pair of objects in a contravariant fashion.
class Obj[DerivedObj <: Obj[DerivedObj]] { /* ... */ }
class Morphism[DerivedMorphism <: Morphism[DerivedMorphism]] { /* ... */ }
I'd like to somehow be able to define:
def mor( from : SomeConcreteObj, to : SomeConcreteObj ) : SomeConcreteMorphism;
but the `obvious' solution of making mor a method of Obj would require Obj to be additionally type-parameterized by Morphism.
What's the Scala way of achieving such a thing?
Soluzione
OK, bad entity relationship on my part. Presumably the implication is that mor is actually a method of some Category class:
trait Cat[O,M] {
def mor( from : O, to : O ) : M;
}
which would seem to solve the problem.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow