a function with one type parameter, where both the argument and the return value are types with a single type parameter, bound to the type parameter of the function.
a function with one type parameter, where both the argument and the return value are types with a single type parameter, bound to the type parameter of the function.
the type bound to use for the argument and return value types
the argument type
the return value type
a map where the types for keys and values share a type parameter with the same bounds.
a map where the types for keys and values share a type parameter with the same bounds. the key and value of each key/value pair are constrained to match on that type parameter. for example, we might have some pet stores that only cater to a single kind of pet:
trait Pet case class Cat(name: String) extends Pet case class Dog(name: String) extends Pet class PetStore[P <: Pet] val catStore1 = new PetStore[Cat] val catStore2 = new PetStore[Cat] val dogStore1 = new PetStore[Dog]
we can use a TypeBoundMap
to store a list of pets of the
appropriate type for every pet store:
var inventories = TypeBoundMap[Pet, PetStore, List] inventories += (catStore1 -> List(Cat("cat11"), Cat("cat12"), Cat("cat13"))) inventories += (catStore2 -> List(Cat("cat21"))) inventories += (dogStore1 -> List(Dog("dog11"), Dog("dog12")))
now we can look up pet lists by pet store, with everything coming back as the expected type:
val cats1: List[Cat] = inventories(catStore1) cats1.size should be (3) val cats2: List[Cat] = inventories(catStore2) cats2.size should be (1) val dogs1: List[Dog] = inventories(dogStore1) dogs1.size should be (2) val cat: Cat = inventories(catStore1).head cat should equal (Cat("cat11")) val dog: Dog = inventories(dogStore1).head dog should equal (Dog("dog11"))
note that the API does not provide methods to add multiple key/value pairs at a time, as each pair needs to be type-checked separately.
(the code presented here is in test class emblem.typeBoundMap.ScaladocSpec
.)
the upper bound on the type parameters passed to the Key
and Val
types
the parameterized type of the keys in the map
the parameterized type of the values in the map
src/test/scala/emblem/typeBoundMap/
for many more examples
mimics a pair found in an ordinary map, but preserves the type parameter equality in the two elements of the pair
mimics a pair found in an ordinary map, but preserves the type parameter equality in the two elements of the pair
the upper bound on the type parameters passed into the A
and B
types of the two elements of this pair
the parameterized type of the first element of this pair
the parameterized type of the second element of this pair
the type param binding both the A
and B
types of the
two elements of this pair
the first element of this type bound pair
the second element of this type bound pair
like a TypeBoundFunction, except that the type bound for the return value is wider than the type bound for the argument.
like a TypeBoundFunction, except that the type bound for the return
value is wider than the type bound for the argument. This is useful for
mapWiden
and mapValuesWiden
methods in TypeKeyMap and
TypeBoundMap that return a map with a wider type bound than the original.
the type bound to use for the argument type
the type bound to use for the return value type
the argument type
the return value type
TypeBoundFunction