I'm scribbling down how to solve the error I got when converting from Java to Kotlin with the Inttellij feature. I may organize it later. I'm a Kotlin beginner and I just erased the error for the time being, so there seems to be a place where it is badly written programmatically. Please let me know if there is a better solution.
For the time being, I attached all <*>
before
var controller: Controller
after
var controller: Controller<*>
<\ > Seems to be an abbreviation for \
before
var controller: Controller
after
var controller: Controller<Data>
This time it was just wasteful, so I erased one side and solved it. There was a method to make a new type and solve it by force, but linguistically, maybe it's about making circular generics.
It disappeared by conversion. In Kotlin, write to access the field as it is. Visibility seems to be read-only by declaring a private set.
before
game.getFinishStatus()
after
game.finishStatus
before
game.setFinishStatus(FinishStatus(true))
after
game.finishStatus=FinishStatus(true)
Overridden methods must have the override modifier. It's usually attached automatically, but there were some who didn't. Can you automatically add method overrides for Objects such as toString? Some of them weren't attached even in their own class. I'm not sure.
before
fun toString(): String {
return text
}
after
override fun toString(): String {
return text
}
Kotlin's default list does not allow you to add or remove elements. If you declare it as List in Java, it will be converted to the default list, so add cannot be used. Rewrite the declaration to MutableList.
before
private var commandList: List<IN>? = null
commandList!!.add(command)//error
after
private var commandList: MutableList<IN>? = null
commandList!!.add(command)//No error
The one whose argument is the index is removeAt
before
shapeList.remove(0)
after
shapeList.removeAt(0)
It disappeared for some reason. If you are doing a safe cast, it seems that there is no problem even if you delete the cast, but there was an error. Maybe it was a cast using Generics.
before
cd//originally(IN)cd //IN is generics
after
cd as IN
Since the type is different between the variable length argument and the normally declared array, it cannot be assigned. Create a new array from variable length arguments and assign.
before
class ControllerGroup(vararg controllers: Controller<*>) {
private val controllers: Array<Controller<*>>
init {
this.controllers = controllers
}
}
after
class ControllerGroup(vararg controllers: Controller<*>) {
private val controllers: Array<Controller<*>>
init {
this.controllers = arrayOf(*controllers)
}
}
Null check related is usually done automatically, but it seems that method arguments are not done?
before
game.addShape(shape)//Shape?That's why it's said no
after
game.addShape(shape!!)
When you want to remove from the Null check an array that is first filled with Null and then filled with values (resulting in no Null). If you are using Arrays.fill or something. Insert when initializing Array
before
val results = arrayOfNulls<RPSResult>(3)
Arrays.fill(results,RPSResult.DRAW)
after
val results = Array<RPSResult>(3,{RPSResult.DRAW})
static import It seems that you can't call a static method without giving a class name with static import.
before
import game.rps.shape.Shape.*
///////////////////////////////
val results = judgeAll(SCISSOR, SCISSOR, SCISSOR, SCISSOR)
after
val results = Shape.judgeAll(SCISSOR, SCISSOR, SCISSOR, SCISSOR)
Add lateinit modifier
before
private var currentShape: Array<Shape?>
after
private lateinit var currentShape: Array<Shape?>
before
Shape.judgeAll(currentShapes)
after
Shape.judgeAll(*currentShapes)
toArray toTypedArray
before
currentList.toArray(arrayOfNulls<Shape>(currentList.size))
after
currentList.toTypedArray()
I've given all the ones I've seen for the time being, so some of them have nothing to do with the information I wrote here, and I've definitely forgotten some. https://qiita.com/k5n/items/c8bf7a507b64f20eebd0 https://qiita.com/tasogarei/items/266ecf02576d48fc69f6 https://stackoverflow.com/questions/46682455/how-to-solve-violation-of-finite-bound-restriction-in-kotlin https://qiita.com/ssuzaki/items/8a550fca6775c1e6e147 https://qiita.com/k5n/items/18adb5c3503a54e96c22 https://qiita.com/ke__kyukyun1828/items/3832d0bf42e6f7ef150a https://qiita.com/koher/items/d9411a00986f14683a3f https://dogwood008.github.io/kotlin-web-site-ja/docs/reference/packages.html https://qiita.com/k5n/items/cc0377b75d8537ef8a85
Recommended Posts