使用“ type”关键字创建模式匹配时,如何处理HKT类型?

时间:2018-09-28 13:31:51

标签: scala types pattern-matching

我有这段代码,当然在编译过程中也会出现此错误

  

...未选中,因为它已通过擦除消除              案例图:MapWithObject => value(地图,路径,索引+ 1)

如何更改模式匹配大小写以摆脱该消息?

type MapWithObject = java.util.HashMap[String, Object]

def value[T : ClassTag](parsedMap: MapWithObject,
                        path: String,
                        index: Int = 0): Option[T] = {
    val key = if (path.contains(".")) path.split("\\.")(index) else path
    parsedMap.asScala.get(key).flatMap({
        case value: T => Some(value)
        case map: MapWithObject => value(map, path, index + 1)
        case _ => None
    })
} 

1 个答案:

答案 0 :(得分:1)

要消除rm -rf /tmp/haste-map-react-native-packager-*的警告,请使用for i in range(0,5): try: click_alert=driver.switch_to_alert() click_alert.accept() continue except TimeoutException: print('wrong value in'+i+'th row . Please check the value ') except NoAlertPresentException: print('i = ', i, 'alert is not present yet, waiting for some time') time.sleep(60) # Delay for 1 minute (60 seconds) except: print "Unexpected error:", sys.exc_info()[0] raise (因为您已经拥有无类型unchecked的无处不在的飞行器,请使用@unchecked注释 不会使情况更糟):

Object

作为对冗长的@unchecked的补救措施,可以使用重命名导入import scala.reflect.ClassTag import scala.collection.JavaConverters._ import java.util.{HashMap => JHashMap} type MapWithObject = JHashMap[String, Object] def value[T: ClassTag]( parsedMap: MapWithObject, path: String, index: Int = 0 ): Option[T] = { val key = if (path.contains(".")) path.split("\\.")(index) else path parsedMap.asScala.get(key).flatMap({ case value: T => Some(value) case map: JHashMap[String @unchecked, Object @unchecked] => value(map, path, index + 1) case _ => None }) } 代替类型定义。