adds a typekey/value pair to this map, returning a new map.
adds a typekey/value pair to this map, returning a new map. the type key is inferred from the type of the supplied value.
PLEASE NOTE: using this method when your Val
type is contravariant in its type parameter will not
do what you might expect! when the compiler infers type parameter [TypeParam <: TypeBound]
from an
argument of type Contra[TypeParam]
, where type Contra
is defined as, e.g., trait Contra[+T]
,
it's always going to infer TypeBound
as the TypeParam
. there seems to be nothing I can do within
TypeKeyMap
to circumvent this. the easiest way to work around this problem is to specify the type key
yourself with the alternate method +.
the type param
the value to add to the map
the type key, which is inferred from the type of value
adds a typekey/value pair to this map, returning a new map.
adds a typekey/value pair to this map, returning a new map.
the type param bounding both the type key and the value
the type param for the value type. this can be any type, provided that
Val[ValTypeParam] <: Val[TypeParam])
the typekey/value pair
a constraint ensuring that Val[ValTypeParam] <: Val[TypeParam])
takes the union of two type key maps with the same type params
takes the union of two type key maps with the same type params
the type key map to union with this type key map
a new type key map with the bindings of this map and that map
retrieves the value which is associated with the given type key.
retrieves the value which is associated with the given type key.
throws java.util.NoSuchElementException when no value is mapped to the supplied type param
the type param binding both the type key and the value
tests whether this TypeKeyMap contains a binding for a type param
tests whether this TypeKeyMap contains a binding for a type param
the type param binding both the type key and the value
true
if there is a binding for type param in this map, false
otherwise.
tests whether this map contains a binding for a key.
tests whether this map contains a binding for a key.
the key
true
if there is a binding for key in this map, false
otherwise.
compares two maps structurally; i.e., checks if all mappings contained in this map are also contained in the other map, and vice versa.
compares two maps structurally; i.e., checks if all mappings contained in this map are also contained in the other map, and vice versa.
the other type key map
true if both maps contain exactly the same mappings, false otherwise.
[use case] applies a function f
to all elements of this type bound map.
applies a function f
to all elements of this type bound map.
the function that is applied for its side-effect to every element. the result of function f
is
discarded
optionally returns the value associated with the given type key
optionally returns the value associated with the given type key
the type param bounding both the type key and the value
an option value containing the value associated with type key in this map, or None if none exists.
returns the value associated with a type key, or a default value if the type key is not contained in the map.
returns the value associated with a type key, or a default value if the type key is not contained in the map.
the type param bounding both the type key and the value
a computation that yields a default value in case no binding for the type key is found in the map
the value associated with type key if it exists, otherwise the result of the default
computation.
tests whether the map is empty.
tests whether the map is empty.
true
if the map does not contain any key/value binding, false
otherwise.
creates a new iterator over all key/value pairs of this map
creates a new iterator over all key/value pairs of this map
the new iterator
collects all keys of this map in an iterable collection.
collects all keys of this map in an iterable collection.
the keys of this map as an iterable.
transforms this type key map by applying a function to every retrieved value.
transforms this type key map by applying a function to every retrieved value.
the new value type for the resulting map
the function used to transform values of this map
a map which maps every key of this map to f(this(key))
transforms this type key map into a type key map with a wider type bound by applying a function to every retrieved value.
transforms this type key map into a type key map with a wider type bound by applying a function to every retrieved value.
the new type bound for the resulting map
the new value type for the resulting map
the function used to transform values of this map.
a map which maps every key of this map to f(this(key))
.
the number of key/value bindings in this map
the number of key/value bindings in this map
a string representation of a TypeKeyMap
a string representation of a TypeKeyMap
collects all values of this map in an iterable collection.
collects all values of this map in an iterable collection.
the values of this map as an iterable.
(typeKeyMap: any2stringadd[TypeKeyMap[TypeBound, Val]]).+(other)
a map where the keys are type keys with an upper bound, and the values have a type parameter with the same bound. The key and value of each key/value pair are constrained to match on that type parameter. For example, suppose we are maintaining an inventory of computer parts:
we can use a
TypeKeyMap
to store a list of parts for each kind of part:now we can look up part lists by part type, with everything coming back as the expected type:
note that the API does not provide
++
or similar methods to add multiple key/value pairs at a time, as each pair needs to be type-checked separately.(code presented here is in TypeKeyMapSpec.scala, up at the top)
the upper bound on the type parameters passed to the TypeKey and Val types
the parameterized type of the values in the map
TypeKeyMapSpec.scala and BaseTypeBoundMapSpec.scala for many more examples
ExtractorPool for an example of how to use type key maps when the value type is more sophisticated than just type with a single type parameter.