4

最初と2番目のコンストラクターが微妙に異なる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)) {

  }

いくつかの言語構造を通じて、これの冗長性を減らすことは可能ですか? 私は代数データ型について考えていましたが、それは適切ではないと感じました - それは「ハッキー」に出くわしました.

4

1 に答える 1