Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false other as Name if (value != other.value) return false return true } override fun hashCode(): Int = value.hashCode() override fun toString(): String = "Name(value='$value')" }
Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false other as Name if (value != other.value) return false return true } override fun hashCode(): Int = value.hashCode() override fun toString(): String = "Name(value='$value')" } ・・・
operator fun component1(): String = firstName operator fun component2(): String = lastName } val nagasawa = FullName("太郎", "長澤") val (first, last) = nagasawa first //=> 太郎 last //=> 長澤 データクラスがこのようなメソッド実装を自動生成する
Exp() class Addition(val x: Exp, val y: Exp): Exp() } val exp: Exp = Addition(Number(1), Number(2)) val sum: Number = eval(exp) sum //=> Exp$Number@2ff4acd0
Exp() class Addition(val x: Exp, val y: Exp): Exp() } val exp: Exp = Addition(Number(1), Number(2)) val sum: Number = eval(exp) sum //=> Exp$Number@2ff4acd0 dataを付けるとコンパイルエラーとなっていた
Int): Exp() data class Addition(val x: Exp, val y: Exp): Exp() } val exp: Exp = Addition(Number(1), Number(2)) val sum: Number = eval(exp) sum //=> Number(value=3)
String, val ruby: String) { fun toName(): Name = Name(value) } interface StringWrapper { val value: String fun component1(): String } data class Name(override val value: String): StringWrapper data class NameWithRuby(override val value: String, val ruby: String): StringWrapper このどちらか、 または両方で対処
thisの文脈が複雑である (内部クラスやレシーバ付き ラムダなど) 名前を付けたい。 let apply run also with 使う場面では ないかも? 人とは違うことをした い! 名前を付けたい。 thisの文脈が複雑である (内部クラスやレシーバ付き ラムダなど) アナタに合う スコープ関数が きっと見つかる! YES NO