【发布时间】:2021-03-01 20:57:48
【问题描述】:
我想做如下的事情:
inline fun<T: Enum<T>> myFunction(enumStr: String){
T.valueOf(enumStr)
//...
}
所以我的泛型参数被限制为枚举类类型,以便我可以访问 valueOf 函数。我收到一条错误消息:Type parameter 'T' cannot have or inherit a companion object, so it cannot be on the left hand side of dot
我理解这意味着我不能在泛型上使用伴随对象函数。有什么方法可以实现我想要的 - 将字符串转换为通用枚举?
【问题讨论】:
标签: kotlin generics enums companion-object