If statement 检查Kotlin、Lua样式中的表达式是否为null?
在中(在Corona SDK开发之外很少使用),您可以对If statement 检查Kotlin、Lua样式中的表达式是否为null?,if-statement,kotlin,lua,If Statement,Kotlin,Lua,在中(在Corona SDK开发之外很少使用),您可以对if语句中的任何表达式求值,如下所示: 如果表达式为null,将返回false 如果表达式是布尔值false,将返回false 其他所有内容都将返回true 例子 if (1) // true if ("Hello World") // true if (instanceOfSomeRandomClass) // true if ( [2, null, "foo"] ) // true if (thisFunctionCallWithRet
if
语句中的任何表达式求值,如下所示:
false
false
,将返回false
true
if (1) // true
if ("Hello World") // true
if (instanceOfSomeRandomClass) // true
if ( [2, null, "foo"] ) // true
if (thisFunctionCallWithReturnNull()) // false
if (0 == 1) // false
如果
恰好也是Kotlin中的一个表达式,那么结合这些特性,我可以看到一些创造性的方法来使用它
科特林是否有类似的标准
我知道你总是可以手动检查if(expression!=null)
,这没什么错,但是if(expression)
比较懒,我喜欢懒:)引用以下内容:
条件表达式的类型必须是kotlin.Boolean的子类型,否则为错误
所以不,在科特林没有直接的等价物
但是,您可以使用Any?
上的自定义命令模拟该行为:
fun Any?.toBoolean() = this != null && this != false
查看它的实际操作(注意输入.toBoolean()
):
输出:
1 is true
Hello World is true
is true
[2, null, foo] is true
null is false
false is false
不过,不确定这是否符合“懒惰者”的条件。引用了:
条件表达式的类型必须是kotlin.Boolean的子类型,否则为错误
所以不,在科特林没有直接的等价物
但是,您可以使用Any?
上的自定义命令模拟该行为:
fun Any?.toBoolean() = this != null && this != false
查看它的实际操作(注意输入.toBoolean()
):
输出:
1 is true
Hello World is true
is true
[2, null, foo] is true
null is false
false is false
但是,不确定这是否符合“lazier”的条件。为了更方便,您可以创建一个更高阶的
luaIf
函数:
sealed class LuaIf<T> {
class True<T>(val value: T) : LuaIf<T>()
object False : LuaIf<Nothing>()
}
inline fun <T> luaIf(condition: Any?, block: () -> T): LuaIf<T> =
if (condition == null || condition == false) False as LuaIf<T>
else True(block())
inline infix fun <T : R, R> LuaIf<T>.els(ifFalse: () -> R): R = when (this) {
is True -> value
False -> ifFalse()
}
不幸的是,每次条件为
true
(或不是null
),都会创建LuaIf.true
实例。您可以通过内联LuaIf
class对其进行优化:
inline class LuaIf<T> @Deprecated(
message = "Not type-safe, use factory method",
replaceWith = ReplaceWith("luaIf(true) { _value }", "package.file.luaIf")
) constructor(val _value: Any?)
object LuaIfFalse
inline fun <T> luaIf(condition: Any?, ifTrue: () -> T): LuaIf<T> =
if (condition == null || condition == false) LuaIf(LuaIfFalse)
else LuaIf(ifTrue())
inline infix fun <T : R, R> LuaIf<T>.els(ifFalse: () -> R): R =
if (_value == LuaIfFalse) ifFalse()
else _value as T
但是由于内联类是实验性的,如果您以一种奇怪的方式使用它们,它们可能会导致一些错误。例如,以下代码抛出ClassCastException:Integer不能强制转换为LuaIf
:
falseLua { luaIf(0) { 0 } } els { 0 }
fun <T> falseLua(other: () -> T): T = luaIf(false) { error("Impossible") } els other
false lua{luaIf(0){0}}els{0}
fun falseLua(other:()->T):T=luaIf(false){error(“不可能”)}els other
我已经询问了一个有关此异常的问题。为了方便起见,您可以创建一个更高阶的
luaIf
函数:
sealed class LuaIf<T> {
class True<T>(val value: T) : LuaIf<T>()
object False : LuaIf<Nothing>()
}
inline fun <T> luaIf(condition: Any?, block: () -> T): LuaIf<T> =
if (condition == null || condition == false) False as LuaIf<T>
else True(block())
inline infix fun <T : R, R> LuaIf<T>.els(ifFalse: () -> R): R = when (this) {
is True -> value
False -> ifFalse()
}
不幸的是,每次条件为
true
(或不是null
),都会创建LuaIf.true
实例。您可以通过内联LuaIf
class对其进行优化:
inline class LuaIf<T> @Deprecated(
message = "Not type-safe, use factory method",
replaceWith = ReplaceWith("luaIf(true) { _value }", "package.file.luaIf")
) constructor(val _value: Any?)
object LuaIfFalse
inline fun <T> luaIf(condition: Any?, ifTrue: () -> T): LuaIf<T> =
if (condition == null || condition == false) LuaIf(LuaIfFalse)
else LuaIf(ifTrue())
inline infix fun <T : R, R> LuaIf<T>.els(ifFalse: () -> R): R =
if (_value == LuaIfFalse) ifFalse()
else _value as T
但是由于内联类是实验性的,如果您以一种奇怪的方式使用它们,它们可能会导致一些错误。例如,以下代码抛出ClassCastException:Integer不能强制转换为LuaIf
:
falseLua { luaIf(0) { 0 } } els { 0 }
fun <T> falseLua(other: () -> T): T = luaIf(false) { error("Impossible") } els other
false lua{luaIf(0){0}}els{0}
fun falseLua(other:()->T):T=luaIf(false){error(“不可能”)}els other
我已经问了一个关于这个例外的问题