This example is simplified.
I have a set of classes like this:
case class KeyMapping[KeyType](k:KeyType)
class WrappedMapping[KeyType](m:T forSome {type T <: KeyMapping[KeyType]}) {
val k:KeyType = ???
}
In the following code the types are correctly inferred:
val w = new WrappedMapping(KeyMapping("key"))
//The statement below gives the correct error
//type mismatch;
// found : w.k.type (with underlying type String) required: Nothing
//val test1:Nothing = w.k
I have no clue how to infer the type correctly for the following:
class Mappings[KeyType, L <: HList](mappings:L) {
val k:KeyType = ???
}
val m = new Mappings(KeyMapping("key1") :: KeyMapping("key2") :: HNil)
// should not compile, k should be of type String
val test2:Nothing = m.k
Is there a way I can infer the KeyType
based on the contents of the HList
?
class WrappedMapping[KeyType](m: KeyMapping[KeyType])
? Right now, that existential type sticks out like a sore thumb, and it definitely makes the question a lot harder to answer. – Kevin WrightWrappedMapping
isn't used inMappings
– Kevin WrightHList
have the same key type? The goal is a little unclear. – Travis BrownKeyType
inMappings
to be inferred with the upper bound of the types of keys. If the only solution for the correct inference is to only allow one type of key, that is fine too. As long as it's inferred from theKeyMappings
in theHList
. – EECOLOR