Why Scala does not have a decltype?

后端 未结 1 1800
故里飘歌
故里飘歌 2021-01-18 22:01

Sometimes one might want to declare x to be of the same type as y. With vals type inference handles this very well, but this does not

相关标签:
1条回答
  • 2021-01-18 22:26

    My first stab:

    class Decl[T] { type Type = T }
    object Decl { def apply[T](x: T) = new Decl[T] }
    

    For example, if we have some variable x whose type we don't want to state explicitly:

    val d = Decl(x)
    type TypeOfX = d.Type
    
    0 讨论(0)
提交回复
热议问题