隐式转换和映射键

时间:2014-06-21 10:36:05

标签: scala type-conversion implicit-conversion

我已经定义了一个枚举

object SupportedCurrencies extends Enumeration {
  type SupportedCurrencies = Value

  val USD, GBP, ARS, AUD, BRL, CAD, CHF, CNY, EUR, JPY, SEK, DKK, NOK = Value
}

并将其隐式转换为字符串

implicit def supportedCurrencyToString(currency: SupportedCurrencies.Value): String = currency.toString

但如果我尝试创建Map [String,Int]

val m: Map[String, Int] = Map(USD -> 1)

我收到错误

type mismatch;
[error]  found   : (helpers.SupportedCurrencies.Value, Int)
[error]  required: (String, Int)
[error]         val m: Map[String, Int] = Map(USD -> 1)

有人能解释这里有什么问题吗?

1 个答案:

答案 0 :(得分:0)

我密切关注错误并得到它

implicit def supportedCurrencyToString(currencyEntry: (helpers.SupportedCurrencies.Value, Int)): (String, Int) = currencyEntry._1.toString -> currencyEntry._2

我需要从一对转换为一对