How to declare a byte array in Scala?

前端 未结 7 1302
心在旅途
心在旅途 2020-12-23 21:22

In Scala, I can declare a byte array this way

val ipaddr: Array[Byte] = Array(192.toByte, 168.toByte, 1.toByte, 9.toByte)

This is too verbo

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-23 21:55

    You can use implicit

    
        implicit def int2byte(int: Int) = {
          int.toByte
        }
    
    

    And that will convert all Int values in scope in places where byte is required.

提交回复
热议问题