使用Lateinit原语包装器 [英] Using lateinit primitives wrappers

查看:67
本文介绍了使用Lateinit原语包装器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的东西:

@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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆