提问人:Stefan 提问时间:10/27/2022 最后编辑:Stefan 更新时间:10/27/2022 访问量:60
Kotlin:将函数参数清理成新变量后将其标记为“不再使用它”
Kotlin: mark function argument after sanitizing it into a new variable as "do not use this anymore"
问:
首先:这个问题对我来说已经解决了。但讨论可能很有趣。
我喜欢代码,所以让我们看看这个函数:
fun foo(path: Path) {
val absPath = path.normalize().absolute() // sanitizing
doSomethingWith(path) // this is unsafe use because path is not sanitized
doSomethingWith(absPath) // this is safe because we are using the sanitized absPath value
}
Kotlin 函数参数始终是 ,因此如果我们想从它的值中派生,我们需要创建一个新变量。
我们可以选择使用新名称或使用旧名称并对其进行注释以不收到警告。val
@Suppress("NAME_SHADOWING")
Name shadowed: ...
我正在寻找类似的东西
fun foo(path: Path) {
val absPath = path.normalize().absolute()
@DoNotUseAnymore path
doSomethingWith(path) // should give a warning/error
doSomethingWith(absPath) // is fine
}
你知道这样的事情吗?或者你认为我在等式的错误一端摆弄,应该学会在使用 -annotation 时不想做坏事?由于我喜欢编码,这就是我的意思:@Suppress
fun foo(path: Path) {
@Suppress("NAME_SHADOWING")
val path = path.normalize().absolute() // sanitizing
doSomethingWith(path) // there is only one sanitized variable so we are safe
}
在某种程度上,这种方法是最干净的......我可能会坚持这一点......我现在应该发布这个问题吗?井。。。也许:)
答: 暂无答案
评论
path
unsanitizedPath