trait Entity:
type Key
object Entity:
type Aux[K] = Entity { type Key = K }
// match type
type EntityKey[T <: Entity] = T match
case Entity.Aux[k] => k // k being lower-case is significant
type Dictionary[T <: Entity] = Map[EntityKey[T], T]
我不得不引入Aux-type,因为匹配类型似乎不适用于细化类型
type EntityKey[T <: Entity] = T match
case Entity { type Key = k } => k // Not found: type k
trait Entity:
type Key
// type class
trait EntityKey[T <: Entity]:
type Out
object EntityKey:
type Aux[T <: Entity, Out0] = EntityKey[T] { type Out = Out0 }
given [K]: EntityKey.Aux[Entity { type Key = K}, K] = null
// replacing the type with a trait
trait Dictionary[T <: Entity](using val entityKey: EntityKey[T]):
type Dict = Map[entityKey.Out, T]
1条答案
按热度按时间3yhwsihp1#
我不得不引入
Aux
-type,因为匹配类型似乎不适用于细化类型请注意,在值级别上匹配类型可能不如类型投影工作得好。
What does Dotty offer to replace type projections?
https://users.scala-lang.org/t/converting-code-using-simple-type-projections-to-dotty/6516
Dotty cannot infer result type of generic Scala function taking type parameter trait with abstract type