使用Lateinit原语包装器 [英] Using lateinit primitives wrappers
问题描述
我有类似的东西:
@LocalServerPort
private lateinit var serverPort: Integer
并且IDEA警告不要使用java.lang.Integer
,而应使用kotlin.Int
.但是kotlin.Int
不能与lateinit
属性一起使用.有办法满足这两个限制吗?
And IDEA warns that java.lang.Integer
should not be used, use kotlin.Int
instead. But kotlin.Int
cannot be used with a lateinit
property. Is there a way to satisfy both restrictions?
更新:我认为每个项目都应该使用-Werror
进行编译.因此,通过@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
添加不必要的垃圾,代码变得很混乱.
Update: I'm compiling with -Werror
, as I think every project should. Therefore, code becomes ridden with @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
adding unnecessary garbage.
推荐答案
是的,代表们.
@LocalServerPort
private var serverPort by Delegates.notNull<Int>()
代理在计算上比lateinit
稍微贵一些.我尽可能使用lateinit
,否则使用上面的方法.上面的代码将与lateinit
完全相同.无需空检查.
Delegates are computationally slightly more expensive than lateinit
. I use lateinit
where I can, otherwise I use the method above. The code above will present exactly the same as lateinit
ie. no null check needed.
这篇关于使用Lateinit原语包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!