Java枚举的Scala类型别名不起作用?

时间:2019-06-24 09:41:43

标签: scala

java类的scala类型别名可以正常工作,即

type JavaDate = org.joda.time.LocalDate

val date = new JavaDate(2019, 6, 20)
println(date)

但是java枚举类型的别名不起作用吗?

val m1 = java.time.Month.JANUARY
println(m1)

type JavaMonth = java.time.Month

val m2 = JavaMonth.FEBRUARY
println(m2)

错误:找不到:值JavaMonth:val m2 = JavaMonth.FEBRUARY

如何对Java枚举使用别名?谢谢!

1 个答案:

答案 0 :(得分:1)

考虑使用import renaming而不是像这样的类型别名:

import java.time.{Month => JavaMonth}
val m2 = JavaMonth.FEBRUARY

类型别名可能更适合于简化更复杂的类型,例如

type Response = Future[Either[Error, Int]]