Hongxu Chen
Hongxu Chen

Reputation: 5350

Is there any way to get Key/Value type from a map in Scala?

In C++ we can get the abstract key/value type (key_type, value_type, http://en.cppreference.com/w/cpp/container/map) from the defined map. This is quite convenient when I need to change the map type later since the dependent type would change accordingly.

In Scala I'm also looking for such a feature. For example, I define a map type:

type MapTy : mutable.Map[Long, Int]

And I hope to return an entry of the map (whose type is Long->Int). The return type depends on MapTy and it had better be explicitly specified in the function signature, written as something like MapTy::key_type->MapTy::value_type. Therefore, when later I change MapTy to

type MapTy : mutable.Map[Int, Int]

The entry type would also change to Int->Int simultaneously.

Is it possible in Scala?

Upvotes: 3

Views: 1010

Answers (2)

Krever
Krever

Reputation: 1467

In Scala generic types are lost in erasure, so it is not possible in runtime. Someone may show how to this, until then here is a workaround:

type Key = Int
type Value = Long
type Map = mutable.Map[Key, Value]

Also, this may be helpful.

Upvotes: 2

Travis Brown
Travis Brown

Reputation: 139028

It entirely possible to do this kind of thing with type members and type refinements in Scala:

scala> case class WrappedMap[K, V](underlying: Map[K, V]) { type E = (K, V) }
defined class WrappedMap

scala> type MapTy = WrappedMap[Long, Int]
defined type alias MapTy

scala> val entry: MapTy#E = 1L -> 0
entry: (Long, Int) = (1,0)

scala> val entry: MapTy#E = 1L -> "foo"
<console>:14: error: type mismatch;
 found   : String("foo")
 required: Int
       val entry: MapTy#E = 1L -> "foo"
                                  ^

Or:

def entries(m: MapTy): List[MapTy#E] = m.underlying.toList

val myEntries: List[MapTy#E] = entries(WrappedMap(Map(1L -> 0)))

The standard collections library for the most part doesn't include type members representing element types, in part because working generically with the type parameters fits better with idiomatic Scala development practices, but there are situations where type members are useful, and if you really want to approximate what you're describing in C++, that may be one of them.

Upvotes: 4

Related Questions