我有一段 Kotlin 代码,其中第一个和第二个构造函数略有不同,见下文
class InstructionPrototype constructor(
val iname: String,
val opcode: Int,
val mnemonicExample: String,
val numericExample: Int,
val description: String,
val format: Format,
val pattern: Pattern,
var type: Type? = null,
var rt: Int? = null,
var funct: Int? = null,
var conditions: Array<(n: Int) -> String?>? = null) {
constructor(
iname: String,
opcode: Int,
mnemonicExample: String,
numericExample: Int,
description: String,
format: Format,
pattern: Pattern,
type: Type?,
rt: Int?,
funct: Int?,
condition: (n: Int) -> String?
): this(iname, opcode, mnemonicExample, numericExample, description,
format, pattern, type, rt, funct, arrayOf(condition)) {
}
是否可以通过某种语言结构来减少这种冗长?我在考虑代数数据类型,但感觉不太合适——它被认为是“hacky”。