Kotlin中的类型安全不';I don’我没有按预期工作
我有这样一个代码:Kotlin中的类型安全不';I don’我没有按预期工作,kotlin,Kotlin,我有这样一个代码: enum class Player { PLAYER, COMPUTER } interface BoardCell { val x: Int val y: Int var player: Player? } data class Cell(val x: Int, val y: Int, var player: Player?, var value: Int) data class BoardCellClass(override val x: In
enum class Player { PLAYER, COMPUTER }
interface BoardCell {
val x: Int
val y: Int
var player: Player?
}
data class Cell(val x: Int, val y: Int, var player: Player?, var value: Int)
data class BoardCellClass(override val x: Int, override val y: Int, override var player: Player?) : BoardCell
data class Request(val board: MutableList<MutableList<BoardCellClass>>? = null, val occupied: MutableList<BoardCellClass>? = null)
class AI(board: MutableList<MutableList<BoardCell>>, private var occupied: MutableList<BoardCell>) {
private var board: MutableList<MutableList<Cell>> = board.map { it.map { Cell(it.x, it.y, it.player, 0) } .toMutableList() } .toMutableList()
}
// in main
val request = call.receive<Request>()
if (request.board == null || request.occupied == null) {
// respond with 403
} else {
val ai = AI(request.board, request.occupied) // Kotlin: Type mismatch: inferred type is MutableList<MutableList<BoardCellClass>>? but MutableList<MutableList<BoardCell>> was expected
// Kotlin: Type mismatch: inferred type is MutableList<BoardCellClass>? but MutableList<BoardCell> was expected
}
enum类播放器{Player,COMPUTER}
接口板单元{
val x:Int
val y:Int
玩家:玩家?
}
数据类单元格(val x:Int,val y:Int,var player:player?,var值:Int)
数据类BoardCellClass(覆盖值x:Int,覆盖值y:Int,覆盖变量player:player?):BoardCell
数据类请求(val板:可变表?=null,val占用:可变表?=null)
AI类(董事会:可变列表,专用var占用:可变列表){
私有var板:MutableList=board.map{it.map{Cell(it.x,it.y,it.player,0)}.toMutableList()}.toMutableList()
}
//大体上
val request=call.receive()
if(request.board==null | | request.occulated==null){
//回复403
}否则{
val ai=ai(request.board,request.occumped)//Kotlin:类型不匹配:推断的类型为MutableList?但应为MutableList
//Kotlin:类型不匹配:推断类型为MutableList?但应为MutableList
}
但它与底部评论中的内容有误。我做错了什么?显然,有一个if语句捕获空值,因此它不应该是MutableList?
,而是MutableList
,不是吗?
另外,MutableList
与MutableList
兼容,因为它实现了该接口,对吗
另外,MutableList
与MutableList
兼容,因为它实现了该接口,对吗
不,不是。这是一个问题。搜索“Kotlin方差”会给你很多解释,但最简单的方法是了解为什么MutableList
不是MutableList
的子类型
val list: MutableList<BoardCellClass> = ...
val list1: MutableList<BoardCell> = list // illegal in actual Kotlin!
list1.add(object : BoardCell { ... }) // would add a BoardCell to a MutableList<BoardCellClass>
val-list:MutableList=。。。
val list1:MutableList=list//在实际Kotlin中非法!
add(object:BoardCell{…})//将向可变列表添加BoardCell
然后可以将相同的逻辑提升一个级别,以查看MutableList
不是MutableList
的子类型
显然,有一个if语句捕获空值,因此它不应该是MutableList?
,而是MutableList
,不是吗
Kotlin smart casts的工作原理并非如此(尽管这种差异在大多数情况下并不相关)。它仍然是MutableList?
类型,但是如果在所需类型为MutableList
的地方使用它,它会自动强制转换。这里所需的类型是MutableList
,因此没有插入强制转换,可空类型显示在错误消息中
另外,MutableList
与MutableList
兼容,因为它实现了该接口,对吗
不,不是。这是一个问题。搜索“Kotlin方差”会给你很多解释,但最简单的方法是了解为什么MutableList
不是MutableList
的子类型
val list: MutableList<BoardCellClass> = ...
val list1: MutableList<BoardCell> = list // illegal in actual Kotlin!
list1.add(object : BoardCell { ... }) // would add a BoardCell to a MutableList<BoardCellClass>
val-list:MutableList=。。。
val list1:MutableList=list//在实际Kotlin中非法!
add(object:BoardCell{…})//将向可变列表添加BoardCell
然后可以将相同的逻辑提升一个级别,以查看MutableList
不是MutableList
的子类型
显然,有一个if语句捕获空值,因此它不应该是MutableList?
,而是MutableList
,不是吗
Kotlin smart casts的工作原理并非如此(尽管这种差异在大多数情况下并不相关)。它仍然是MutableList?
类型,但是如果在所需类型为MutableList
的地方使用它,它会自动强制转换。这里所需的类型是MutableList
,因此没有插入强制转换,可空类型显示在错误消息中
MutableList>与兼容
MutableList>,因为它实现了
接口,对吗
不。在你的情况下,你可以使用关键字
MutableList>与兼容
MutableList>,因为它实现了
接口,对吗
不。在你的情况下,你可以使用关键字
您能显示什么是
call
和什么是receive()
返回的吗?您能显示什么是call
和什么是receive()
返回的吗?
if (request.board != null && request.occupied != null){
val ai = AI(request.board, request.occupied)
} else {
// respond with 403
}