0
我定義了一個階枚舉匹配任何枚舉值:斯卡拉在case語句
object SalesChannelType extends scala.Enumeration {
type SalesChannelType = Value
val SALES_CHANNEL_1, SALES_CHANNEL_2 = Value
}
class SalesChannelType extends TypeReference[SalesChannelType.type]
現在我想編寫在一個單一的情況下匹配任何枚舉值匹配的語句,是這樣的:
SalesChannelType.SALES_CHANNEL_1 match {
case SalesChannelType => println(_)
case _ => println("specified sales channel does not exist")
這個想法是爲第一個case語句打印銷售渠道,如果它在枚舉中定義的話。否則,應該調用第二個case語句。目前上面的代碼不會與錯誤編譯Pattern type is incompatible with expected type, found SalesChannel.type required SalesChannel.Value
如果您的程序輸入得體,沒有不存在的銷售渠道可以達到該模式匹配。或者我錯過了什麼? –
是的,你說得很好,這是一個不必要的匹配語句。仍然想知道它是否可能? – novon
你可以製作一個[自定義模式/提取器](http://docs.scala-lang.org/tutorials/tour/extractor-objects.html)。 –