fold
inline fun <R> CommandSender.fold(ifIsSystem: SystemCommandSender.() -> R, ifIsUser: UserCommandSender.() -> R, otherwise: CommandSender.() -> R = { error("CommandSender ${this::class.qualifiedName} is not supported") }): R
折叠 CommandSender 的可能性.
当 this 为 SystemCommandSender 时执行 ifIsSystem
当 this 为 UserCommandSender 时执行 ifIsUser
否则执行 otherwise
示例
// 当一个指令执行过程出错
val exception: Exception = ...
sender.fold(
ifIsSystem = { // this: SystemCommandSender
sendMessage(exception.stackTraceToString()) // 展示整个 stacktrace
},
ifIsUser = { // this: UserCommandSender
sendMessage(exception.message.toString()) // 只展示 Exception.message
}
)
Content copied to clipboard
Return
ifIsSystem, ifIsUser 或 otherwise 执行结果.