Convert java.lang.Boolean to Scala Boolean

后端 未结 3 1513
醉梦人生
醉梦人生 2021-01-25 17:03

I am currently working on a Scala application which utilizes Spring-Boot and Swagger to send & receive REST-calls.

Swagger and Spring-Boot are pure Java-projects an

3条回答
  •  盖世英雄少女心
    2021-01-25 17:55

    1. You don't need to write out java.lang.blah every time in

      ParamsAsJava(java.lang.Boolean.FALSE, java.lang.Boolean.TRUE)
      

      Just use

      ParamsAsJava(false, true)
      

      instead. Autoboxing hasn't gone anywhere.

    2. to get rid of toScala, define an implicit conversion in Params companion object:

      object Params {
        implicit def params_j2s(p: ParamsAsJava): Params = p.toScala()
      }
      

      now you can write:

      val test1: Params = ParamsAsJava(true, false)
      

      and, of course, if you don't define this variable in vacuum, but pass it to a method instead, the right type will be inferred automatically, and the object will be converted implicitly.

    3. No need to use parens () in def toScala(), the method has no side effects.

提交回复
热议问题