Upgrade to Pro — share decks privately, control downloads, hide ads and more …

KotlinのDelegated Propertiesを活用してAndroidアプリ開発をもっ...

KotlinのDelegated Propertiesを活用してAndroidアプリ開発をもっと便利にする

Kotlinには委譲プロパティ(Delegetaed proprties)という仕組みがあります。
標準ライブラリのlazyや、AndroidXのActivityやFragmentに実装されているviewModelなどで普段からあまり意識せずに利用している方も多いと思います。
本セッションでは、標準ライブラリに含まれている委譲プロパティ、および拙作のライブラリであるKotprefなどの事例を交えながら、委譲プロパティの仕組み、Kotlinでの実現方法、活用事例や自作する際の方法などをお話します。

本セッションに含まれる内容
- KotlinのDelegated Propertiesとは
- Delegated Propertiesの仕組み
- 標準ライブラリで提供されるDelegated Properties
- 作って見ることでより深く理解する
- Tips

chibatching

March 17, 2020
Tweet

More Decks by chibatching

Other Decks in Programming

Transcript

  1. %FMFHBUFE1SPQFSUJFTͱ͸ class DelegateSample { var myProp: String by MyDelegate() var

    someProp: String by someDelegate() private fun someDelegate() = SomeDelegate() }
  2. %FMFHBUFE1SPQFSUJFTͱ͸ class DelegateSample { var myProp: String by MyDelegate() var

    someProp: String by someDelegate() private fun someDelegate() = SomeDelegate() } CZͷޙʹҕৡઌͷ ΦϒδΣΫτΛࢦఆ͢Δ
  3. %FMFHBUFE1SPQFSUJFTͱ͸ class DelegateSample { var myProp: String by MyDelegate() var

    someProp: String by someDelegate() private fun someDelegate() = SomeDelegate() } ҕৡઌΦϒδΣΫτΛ औಘ͢Δؔ਺౳Ͱ΋Մ
  4. %FMFHBUFE1SPQFSUJFTͱ͸ class DelegateSample { private val myProp$delegate = MyDelegate() var

    myProp: String get() = myProp$delegate.getValue(this, this::myProp) set(value: String) = myProp$delegate.setValue(this, this::myProp, value) }
  5. %FMFHBUFE1SPQFSUJFTͱ͸ class DelegateSample { private val myProp$delegate = MyDelegate() var

    myProp: String get() = myProp$delegate.getValue(this, this::myProp) set(value: String) = myProp$delegate.setValue(this, this::myProp, value) } ҕৡઌͷΦϒδΣΫτΛQSJWBUFͳ ϓϩύςΟͱͯ͠อ࣋
  6. %FMFHBUFE1SPQFSUJFTͱ͸ class DelegateSample { private val myProp$delegate = MyDelegate() var

    myProp: String get() = myProp$delegate.getValue(this, this::myProp) set(value: String) = myProp$delegate.setValue(this, this::myProp, value) } ҕৡઌΦϒδΣΫτ͕ HFUͷॲཧΛ͢Δ
  7. %FMFHBUFE1SPQFSUJFTͱ͸ class DelegateSample { private val myProp$delegate = MyDelegate() var

    myProp: String get() = myProp$delegate.getValue(this, this::myProp) set(value: String) = myProp$delegate.setValue(this, this::myProp, value) } ಉ༷ʹҕৡઌΦϒδΣΫτ͕ TFUͷॲཧΛ͢Δ
  8. MB[Z • ϓϩύςΟͷॳظԽΛ࠷ॳͷࢀর·Ͱ஗Ԇ val lazyProperty: String by lazy { "initialized

    at ${System.currentTimeMillis()}" } println(System.currentTimeMillis()) // 1581485501007 delay(500)
  9. MB[Z • ϓϩύςΟͷॳظԽΛ࠷ॳͷࢀর·Ͱ஗Ԇ val lazyProperty: String by lazy { "initialized

    at ${System.currentTimeMillis()}" } println(System.currentTimeMillis()) // 1581485501007 delay(500) println(lazyProperty) // initialized at 1581485501514 ࠷ॳͷࢀর࣌ʹϒϩοΫ͕࣮ߦ͞Ε ϓϩύςΟ͕ॳظԽ͞ΕΔ NT
  10. MB[Z • ϓϩύςΟͷॳظԽΛ࠷ॳͷࢀর·Ͱ஗Ԇ val lazyProperty: String by lazy { "initialized

    at ${System.currentTimeMillis()}" } println(System.currentTimeMillis()) // 1581485501007 delay(500) println(lazyProperty) // initialized at 1581485501514 delay(500) println(lazyProperty)
  11. MB[Z • ϓϩύςΟͷॳظԽΛ࠷ॳͷࢀর·Ͱ஗Ԇ val lazyProperty: String by lazy { "initialized

    at ${System.currentTimeMillis()}" } println(System.currentTimeMillis()) // 1581485501007 delay(500) println(lazyProperty) // initialized at 1581485501514 delay(500) println(lazyProperty) // initialized at 1581485501514 ճ໨Ҏ߱ͷΞΫηεͰ͸࠷ॳʹ ࣮ߦͨ͠ͱ͖ͷ஋ʹͳΔ NT
  12. MB[Z • +BWB෩ͳΞϓϩʔν private var param: String? = null override

    fun onCreate(savedInstanceState: Bundle?) { param = intent.getStringExtra("param") Log.d(TAG, param) }
  13. MB[Z • +BWB෩ͳΞϓϩʔν private var param: String? = null override

    fun onCreate(savedInstanceState: Bundle?) { param = intent.getStringExtra("param") ?: throw IllegalArgumentException() Log.d(TAG, param) }
  14. MB[Z • +BWB෩ͳΞϓϩʔν private var param: String? = null override

    fun onCreate(savedInstanceState: Bundle?) { param = intent.getStringExtra("param") ?: throw IllegalArgumentException() Log.d(TAG, param) }
  15. MB[Z • +BWB෩ͳΞϓϩʔν private var param: String? = null override

    fun onCreate(savedInstanceState: Bundle?) { param = intent.getStringExtra("param") ?: throw IllegalArgumentException() Log.d(TAG, param) }
  16. MB[Z • +BWB෩ͳΞϓϩʔν private var param: String? = null override

    fun onCreate(savedInstanceState: Bundle?) { param = intent.getStringExtra("param") ?: throw IllegalArgumentException() Log.d(TAG, param!!) } OVMMBCMFͳͷͰOPOOVMM΁ͷ ม׵͕ඞཁʹͳΔ
  17. MB[Z • MBUFJOJUΛ࢖ͬͨํ๏ private lateinit var param: String override fun

    onCreate(savedInstanceState: Bundle?) { param = intent.getStringExtra("param") ?: throw IllegalArgumentException() Log.d(TAG, param) }
  18. MB[Z • MBUFJOJUΛ࢖ͬͨํ๏ private lateinit var param: String override fun

    onCreate(savedInstanceState: Bundle?) { param = intent.getStringExtra("param") ?: throw IllegalArgumentException() Log.d(TAG, param) }
  19. MB[Z • MBUFJOJUΛ࢖ͬͨํ๏ private lateinit var param: String override fun

    onCreate(savedInstanceState: Bundle?) { param = intent.getStringExtra("param") ?: throw IllegalArgumentException() Log.d(TAG, param) } private fun someOperation() { param = "overwritten with wrong value" } NVUBCMFͳͷͰ༧ظ͠ͳ͍ ஋ͷॻ͖׵͕͑ൃੜ͠͏Δ
  20. MB[Z • MBUFJOJUΛ࢖ͬͨํ๏ private lateinit var param: String override fun

    onCreate(savedInstanceState: Bundle?) { param = intent.getStringExtra("param") ?: throw IllegalArgumentException() Log.d(TAG, param) } private fun someOperation() { if (!::param.isInitialized) { param = intent.getStringExtra("param") } textView.text = param } ෳ਺ͷՕॴ͔Βࢀর͞ΕΔ৔߹ɺ ॳظԽ͞Ε͍ͯΔ͜ͱΛ֬ೝ͢Δ ඞཁ͕͋Δ৔߹΋ʜ
  21. MB[Z • MB[ZͳΒJNNVUBCMFͳϓϩύςΟͰ஗ԆॳظԽͰ͖Δ private val param: String by lazy {

    intent.getStringExtra("param") ?: throw IllegalArgumentException() } override fun onCreate(savedInstanceState: Bundle?) { Log.d(TAG, param) }
  22. %FMFHBUFTOPU/VMM • MBUFJOJU  private var param: String by Delegates.notNull()

    override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) param = intent.getStringExtra("param") ?: throw IllegalArgumentException() Log.d(TAG, param) }
  23. %FMFHBUFTOPU/VMM • +BWBͱͷޓ׵ੑʹ஫ҙ public final class Sample { @NotNull private

    final ReadWriteProperty notNull$delegate; @NotNull public String lateinitVar; public final void setNotNull(@NotNull String var1) {} @NotNull public final String getNotNull() {} public final void setLateinitVar(@NotNull String var1) {} @NotNull public final String getLateinitVar() {} }
  24. %FMFHBUFTOPU/VMM • +BWBͱͷޓ׵ੑʹ஫ҙ public final class Sample { @NotNull private

    final ReadWriteProperty notNull$delegate; @NotNull public String lateinitVar; public final void setNotNull(@NotNull String var1) {} @NotNull public final String getNotNull() {} public final void setLateinitVar(@NotNull String var1) {} @NotNull public final String getLateinitVar() {} } QVCMJD͔ͭະॳظԽͷ ϑΟʔϧυͱͯ࣋ͭ͠
  25. %FMFHBUFTOPU/VMM • +BWBͱͷޓ׵ੑʹ஫ҙ public final class Sample { @NotNull private

    final ReadWriteProperty notNull$delegate; @NotNull public String lateinitVar; public final void setNotNull(@NotNull String var1) {} @NotNull public final String getNotNull() {} public final void setLateinitVar(@NotNull String var1) {} @NotNull public final String getLateinitVar() {} } QSJWBUF͔ͭGJOBMͰϓϩύςΟΛ ௚઀ϑΟʔϧυͱͯ࣋ͨ͠ͳ͍
  26. %FMFHBUFTOPU/VMM public final class Sample { @NotNull @Inject private final

    ReadWriteProperty notNull$delegate; @NotNull @Inject public String lateinitVar; public final void setNotNull(@NotNull String var1) {} public final String getNotNull() {} public final void setLateinitVar(@NotNull String var1) {} public final String getLateinitVar() {} }
  27. %FMFHBUFTOPU/VMM public final class Sample { @NotNull @Inject private final

    ReadWriteProperty notNull$delegate; @NotNull @Inject public String lateinitVar; public final void setNotNull(@NotNull String var1) {} public final String getNotNull() {} public final void setLateinitVar(@NotNull String var1) {} public final String getLateinitVar() {} }
  28. %FMFHBUFTOPU/VMM public final class Sample { @NotNull @Inject private final

    ReadWriteProperty notNull$delegate; @NotNull @Inject public String lateinitVar; public final void setNotNull(@NotNull String var1) {} public final String getNotNull() {} public final void setLateinitVar(@NotNull String var1) {} public final String getLateinitVar() {} } ϑΟʔϧυ*OKFDUJPOͰ͖ͳ͍
  29. %FMFHBUFTOPU/VMM public final class Sample { @NotNull @Inject private final

    ReadWriteProperty notNull$delegate; public final void setNotNull(@NotNull String var1) {} public final String getNotNull() {} }
  30. %FMFHBUFTOPU/VMM public final class Sample { @NotNull private final ReadWriteProperty

    notNull$delegate; @Inject public final void setNotNull(@NotNull String var1) {} public final String getNotNull() {} }
  31. %FMFHBUFTPCTFSWBCMFWFUPBCMF var inputtedText: String by Delegates.observable("init") { property, old, new

    -> println("inputted $inputtedText") } var positiveNumber: Long by Delegates.vetoable(1L) { property, old, new -> new > 0L }
  32. %FMFHBUFTPCTFSWBCMFWFUPBCMF var inputtedText: String by Delegates.observable("init") { property, old, new

    -> println("inputted $inputtedText") } var positiveNumber: Long by Delegates.vetoable(1L) { property, old, new -> new > 0L } ஋ͷ୅ೖޙʹϒϩοΫͷॲཧΛ࣮ߦ
  33. %FMFHBUFTPCTFSWBCMFWFUPBCMF var inputtedText: String by Delegates.observable("init") { property, old, new

    -> println("inputted $inputtedText") } var positiveNumber: Long by Delegates.vetoable(1L) { property, old, new -> new > 0L } ஋ͷ୅ೖલʹϒϩοΫΛ࣮ߦ͠ USVFΛฦͨ͠ͱ͖ͷΈ୅ೖ͕ߦΘΕΔ
  34. 4UPSJOH1SPQFSUJFTJOB.BQ class User(map: Map<String, Any>) { val name: String by

    map val age: Int by map } Ωʔ͕4USJOHͷ.BQΛ ҕৡઌʹࢦఆ͢Δ
  35. 4UPSJOH1SPQFSUJFTJOB.BQ class User(map: Map<String, Any>) { val name: String by

    map val age: Int by map } val user = User( mapOf( "name" to "chibatching", "age" to 34 ) ) Ωʔ͕ϓϩύςΟ໊ͱ Ұக͢ΔΑ͏ͳ.BQΛ౉͢
  36. 4UPSJOH1SPQFSUJFTJOB.BQ class User(map: Map<String, Any>) { val name: String by

    map val age: Int by map } val user = User( mapOf( "name" to "chibatching", "age" to 34 ) ) println("name: ${user.name}, age: ${user.age}") // name: chibatching, age: 34
  37. 4UPSJOH1SPQFSUJFTJOB.BQ class User(map: Map<String, Any>) { val name: String by

    map val age: Int by map } val user = User( mapOf( "name" to "chibatching", "age" to 34 ) )
  38. 4UPSJOH1SPQFSUJFTJOB.BQ class User(map: MutableMap<String, Any>) { var name: String by

    map var age: Int by map } val user = User( mutableMapOf( "name" to "chibatching", "age" to 34 ) )
  39. 4UPSJOH1SPQFSUJFTJOB.BQ class User(map: MutableMap<String, Any>) { var name: String by

    map var age: Int by map } val user = User( mutableMapOf( "user" to "chibatching", "age" to 34 ) ) user.name = "chibatching Jr" user.age = 4 // {name=chibatching Jr, age=4}
  40. %FMFHBUFE1SPQFSUJFTͷجຊ class PreferenceDelegate { operator fun getValue( thisRef: Any?, property:

    KProperty<*>): String { return "delegated value" } operator fun setValue( thisRef: Any?, property: KProperty<*>, value: String) { } }
  41. %FMFHBUFE1SPQFSUJFTͷجຊ class PreferenceDelegate { operator fun getValue( thisRef: Any?, property:

    KProperty<*>): String { return "delegated value" } operator fun setValue( thisRef: Any?, property: KProperty<*>, value: String) { } } HFU7BMVFʹ͸ HFUUFSͷॲཧΛ࣮૷͢Δ
  42. %FMFHBUFE1SPQFSUJFTͷجຊ class PreferenceDelegate { operator fun getValue( thisRef: Any?, property:

    KProperty<*>): String { return "delegated value" } operator fun setValue( thisRef: Any?, property: KProperty<*>, value: String) { } } TFU7BMVFʹ͸ TFUUFSͷॲཧΛ࣮૷͢Δ
  43. %FMFHBUFE1SPQFSUJFTͷجຊ class PreferenceDelegate { operator fun getValue( thisRef: Any?, property:

    KProperty<*>): String { return "delegated value" } operator fun setValue( thisRef: Any?, property: KProperty<*>, value: String) { } }
  44. %FMFHBUFE1SPQFSUJFTͷجຊ class PreferenceDelegate : ReadWriteProperty<Any?, String> { operator fun getValue(

    thisRef: Any?, property: KProperty<*>): String { return "delegated value" } operator fun setValue( thisRef: Any?, property: KProperty<*>, value: String) { } } NVUBCMF3FBE8SJUF1SPQFSUZ JNNVUBCMF3FBE0OMZ1SPQFSUZ
  45. %FMFHBUFE1SPQFSUJFTͷجຊ class PreferenceDelegate : ReadWriteProperty<Any?, String> { override operator fun

    getValue( thisRef: Any?, property: KProperty<*>): String { return "delegated value" } override operator fun setValue( thisRef: Any?, property: KProperty<*>, value: String) { } }
  46. %FMFHBUFE1SPQFSUJFTͷجຊ class PreferenceDelegate : ReadWriteProperty<Any?, String> { override operator fun

    getValue( thisRef: Any?, property: KProperty<*>): String { return "delegated value" } override operator fun setValue( thisRef: Any?, property: KProperty<*>, value: String) { } }
  47. class PreferenceDelegate( fileName: String, context: Context, private val key: String,

    private val default: String ) : ReadWriteProperty<Any?, String> { private val preference = context.getSharedPreferences(fileName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { return preference.getString(key, default)!! } 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ
  48. class PreferenceDelegate( fileName: String, context: Context, private val key: String,

    private val default: String ) : ReadWriteProperty<Any?, String> { private val preference = context.getSharedPreferences(fileName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { return preference.getString(key, default)!! } 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ
  49. class PreferenceDelegate( fileName: String, context: Context, private val key: String,

    private val default: String ) : ReadWriteProperty<Any?, String> { private val preference = context.getSharedPreferences(fileName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { return preference.getString(key, default)!! } 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ
  50. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class MainActivity : AppCompatActivity() { private var userName: String

    by PreferenceDelegate("user_settings", this, "user_name", "") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) findViewById<TextView>(R.id.user_name).text = userName findViewById<Button>(R.id.save_button).setOnClickListener { userName = findViewById<EditText>(R.id.input).text.toString() } }
  51. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class MainActivity : AppCompatActivity() { private var userName: String

    by PreferenceDelegate("user_settings", this, "user_name", "") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) findViewById<TextView>(R.id.user_name).text = userName findViewById<Button>(R.id.save_button).setOnClickListener { userName = findViewById<EditText>(R.id.input).text.toString() } }
  52. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class MainActivity : AppCompatActivity() { private var userName: String

    by PreferenceDelegate("user_settings", this, "user_name", "") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) findViewById<TextView>(R.id.user_name).text = userName findViewById<Button>(R.id.save_button).setOnClickListener { userName = findViewById<EditText>(R.id.input).text.toString() } }
  53. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( fileName: String, context: Context, private val key:

    String, private val default: String ) : ReadWriteProperty<Any?, String> { private val preference = context.getSharedPreferences(fileName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { return preference.getString(key, default)!! }
  54. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( fileName: String, context: Context, private val key:

    String, private val default: String ) : ReadWriteProperty<Any?, String> { private val preference = context.getSharedPreferences(fileName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { return preference.getString(key, default)!! }
  55. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( fileName: String, context: Context, private val key:

    String, private val default: String ) : ReadWriteProperty<Any?, String> { private val preference = context.getSharedPreferences(fileName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { return preference.getString(key, default)!! } Ҿ਺ͷ,1SPQFSUZ͸ ҕৡݩϓϩύςΟ
  56. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( fileName: String, context: Context, private val key:

    String, private val default: String ) : ReadWriteProperty<Any?, String> { private val preference = context.getSharedPreferences(fileName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { return preference.getString(key, default)!! }
  57. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( fileName: String, context: Context, private val key:

    String, private val default: String ) : ReadWriteProperty<Any?, String> { private val preference = context.getSharedPreferences(fileName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { return preference.getString(property.name, default)!! }
  58. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( fileName: String, context: Context, private val key:

    String, private val default: String ) : ReadWriteProperty<Any?, String> { private val preference = context.getSharedPreferences(fileName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { return preference.getString(property.name, default)!! }
  59. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( fileName: String, context: Context, private val default:

    String ) : ReadWriteProperty<Any?, String> { private val preference = context.getSharedPreferences(fileName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { return preference.getString(property.name, default)!! }
  60. • ϓϩύςΟͷ৘ใ͸,1SPQFSUZ͔ΒऔಘͰ͖Δ • ͞·͟·ͳ৘ใ͕औಘՄೳ • OBNF • PQFOGJOBM • FUD

    • OBNFҎ֎ͷ৘ใ͸LPUMJOSFGMFDUύοέʔδ͕ඞཁ 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ
  61. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class MainActivity : AppCompatActivity() { private var userName: String

    by PreferenceDelegate("user_settings", this, "user_name", "") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) findViewById<TextView>(R.id.user_name).text = userName findViewById<Button>(R.id.save_button).setOnClickListener { userName = findViewById<EditText>(R.id.input).text.toString() } }
  62. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class MainActivity : AppCompatActivity() { private var userName: String

    by PreferenceDelegate("user_settings", this, "") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) findViewById<TextView>(R.id.user_name).text = userName findViewById<Button>(R.id.save_button).setOnClickListener { userName = findViewById<EditText>(R.id.input).text.toString() } }
  63. • ผͷΫϥεʹ੾Γग़͢ 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class UserSettings(context: Context) { private val fileName

    = "user_settings" var userName by PreferenceDelegate(fileName, context, "") }
  64. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class MainActivity : AppCompatActivity() { private var userName: String

    by PreferenceDelegate("user_settings", this, "") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) findViewById<TextView>(R.id.user_name).text = userName findViewById<Button>(R.id.save_button).setOnClickListener { userName = findViewById<EditText>(R.id.input).text.toString() } }
  65. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class MainActivity : AppCompatActivity() { private val userSettings by

    lazy { UserSettings(this) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) findViewById<TextView>(R.id.user_name).text = userName findViewById<Button>(R.id.save_button).setOnClickListener { userName = findViewById<EditText>(R.id.input).text.toString() } }
  66. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class MainActivity : AppCompatActivity() { private val userSettings by

    lazy { UserSettings(this) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) findViewById<TextView>(R.id.user_name).text = userName findViewById<Button>(R.id.save_button).setOnClickListener { userName = findViewById<EditText>(R.id.input).text.toString() } }
  67. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class MainActivity : AppCompatActivity() { private val userSettings by

    lazy { UserSettings(this) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) findViewById<TextView>(R.id.user_name).text = userSettings.userName findViewById<Button>(R.id.save_button).setOnClickListener { userSettings.userName = findViewById<EditText>(R.id.input).text.toString() } }
  68. • ϓϩύςΟ͕૿͑Δͱʜ 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class UserSettings(context: Context) { private val fileName

    = "user_settings" var userName by PreferenceDelegate(fileName, context, "") var iconUrl by PreferenceDelegate(fileName, context, "") var coverPhoto by PreferenceDelegate(fileName, context, "") }
  69. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class UserSettings(context: Context) { private val fileName = "user_settings"

    var userName by PreferenceDelegate(fileName, context, "") var iconUrl by PreferenceDelegate(fileName, context, "") var coverPhoto by PreferenceDelegate(fileName, context, "") }
  70. • Ϋϥε໊Λͦͷ··ϑΝΠϧ໊ʹͯ͠΋Αͦ͞͏ 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class UserSettings(context: Context) { private val fileName

    = "user_settings" var userName by PreferenceDelegate(fileName, context, "") var iconUrl by PreferenceDelegate(fileName, context, "") var coverPhoto by PreferenceDelegate(fileName, context, "") }
  71. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( fileName: String, context: Context, private val default:

    String ) : ReadWriteProperty<Any?, String> { private val preference = context.getSharedPreferences(fileName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { return preference.getString(property.name, default)!! }
  72. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( fileName: String, context: Context, private val default:

    String ) : ReadWriteProperty<Any?, String> { private val preference = context.getSharedPreferences(fileName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { return preference.getString(property.name, default)!! } ϓϩύςΟ͕એݴ͞Ε͍ͯΔ ΫϥεΠϯελϯε
  73. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( fileName: String, context: Context, private val default:

    String ) : ReadWriteProperty<Any?, String> { private val preference = context.getSharedPreferences(fileName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { return preference.getString(property.name, default)!! }
  74. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( fileName: String, context: Context, private val default:

    String ) : ReadWriteProperty<Any?, String> { private fun getPreferenceForClass(thisRef: Any?) = context.getSharedPreferences( thisRef?.javaClass?.simpleName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { return preference.getString(property.name, default)!! }
  75. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( fileName: String, context: Context, private val default:

    String ) : ReadWriteProperty<Any?, String> { private fun getPreferenceForClass(thisRef: Any?) = context.getSharedPreferences( thisRef?.javaClass?.simpleName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { return preference.getString(property.name, default)!! }
  76. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( fileName: String, context: Context, private val default:

    String ) : ReadWriteProperty<Any?, String> { private fun getPreferenceForClass(thisRef: Any?) = context.getSharedPreferences( thisRef?.javaClass?.simpleName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { val preference = getPreferenceForClass(thisRef) return preference.getString(property.name, default)!! }
  77. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( fileName: String, context: Context, private val default:

    String ) : ReadWriteProperty<Any?, String> { private fun getPreferenceForClass(thisRef: Any?) = context.getSharedPreferences( thisRef?.javaClass?.simpleName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { val preference = getPreferenceForClass(thisRef) return preference.getString(property.name, default)!! }
  78. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( context: Context, private val default: String )

    : ReadWriteProperty<Any?, String> { private fun getPreferenceForClass(thisRef: Any?) = context.getSharedPreferences( thisRef?.javaClass?.simpleName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { val preference = getPreferenceForClass(thisRef) return preference.getString(property.name, default)!! }
  79. • ϑΝΠϧ໊ΛলུͰ͖ΔΑ͏ʹͳͬͨ 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class UserSettings(context: Context) { private val fileName

    = "user_settings" var userName by PreferenceDelegate(fileName, context, "") var iconUrl by PreferenceDelegate(fileName, context, "") var coverPhoto by PreferenceDelegate(fileName, context, "") }
  80. • ϑΝΠϧ໊ΛলུͰ͖ΔΑ͏ʹͳͬͨ 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class UserSettings(context: Context) { var userName by

    PreferenceDelegate(context, "") var iconUrl by PreferenceDelegate(context, "") var coverPhoto by PreferenceDelegate(context, "") }
  81. • $POUFYU΋লུͰ͖Δͱخ͍͠ 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class UserSettings(context: Context) { var userName by

    PreferenceDelegate(context, "") var iconUrl by PreferenceDelegate(context, "") var coverPhoto by PreferenceDelegate(context, "") }
  82. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class UserSettings(context: Context) { var userName by PreferenceDelegate(context, "")

    var iconUrl by PreferenceDelegate(context, "") var coverPhoto by PreferenceDelegate(context, "") }
  83. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class UserSettings(context: Context) { var userName by PreferenceDelegate(context, "")

    var iconUrl by PreferenceDelegate(context, "") var coverPhoto by PreferenceDelegate(context, "") } interface PreferenceModel { val context: Context }
  84. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class UserSettings(override val context: Context) : PreferenceModel { var

    userName by PreferenceDelegate(context, "") var iconUrl by PreferenceDelegate(context, "") var coverPhoto by PreferenceDelegate(context, "") } interface PreferenceModel { val context: Context }
  85. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( context: Context, private val default: String )

    : ReadWriteProperty<Any?, String> { private fun getPreferenceForClass(thisRef: Any?) = context.getSharedPreferences( thisRef?.javaClass?.simpleName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { val preference = getPreferenceForClass(thisRef) return preference.getString(property.name, default)!! }
  86. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( context: Context, private val default: String )

    : ReadWriteProperty<Any?, String> { private fun getPreferenceForClass(thisRef: Any?) = context.getSharedPreferences( thisRef?.javaClass?.simpleName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: Any?, property: KProperty<*> ): String { val preference = getPreferenceForClass(thisRef) return preference.getString(property.name, default)!! }
  87. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( context: Context, private val default: String )

    : ReadWriteProperty<PreferenceModel, String> { private fun getPreferenceForClass(thisRef: Any?) = context.getSharedPreferences( thisRef?.javaClass?.simpleName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: PreferenceModel, property: KProperty<*> ): String { val preference = getPreferenceForClass(thisRef) return preference.getString(property.name, default)!! } ܕΛࢦఆ͢Δ͜ͱͰ ಛఆͷΫϥε಺ͰͷΈ࢖͑ΔΑ͏ ੍ݶ͢Δ͜ͱ͕Ͱ͖Δ
  88. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( context: Context, private val default: String )

    : ReadWriteProperty<PreferenceModel, String> { private fun getPreferenceForClass(thisRef: Any?) = context.getSharedPreferences( thisRef?.javaClass?.simpleName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: PreferenceModel, property: KProperty<*> ): String { val preference = getPreferenceForClass(thisRef) return preference.getString(property.name, default)!! }
  89. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( context: Context, private val default: String )

    : ReadWriteProperty<PreferenceModel, String> { private fun getPreferenceForClass(thisRef: Any?) = context.getSharedPreferences( thisRef?.javaClass?.simpleName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: PreferenceModel, property: KProperty<*> ): String { val preference = getPreferenceForClass(thisRef) return preference.getString(property.name, default)!! }
  90. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( context: Context, private val default: String )

    : ReadWriteProperty<PreferenceModel, String> { private fun getPreferenceForClass(thisRef: PreferenceModel) = thisRef.context.getSharedPreferences( thisRef.javaClass.simpleName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: PreferenceModel, property: KProperty<*> ): String { val preference = getPreferenceForClass(thisRef) return preference.getString(property.name, default)!! }
  91. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( context: Context, private val default: String )

    : ReadWriteProperty<PreferenceModel, String> { private fun getPreferenceForClass(thisRef: PreferenceModel) = thisRef.context.getSharedPreferences( thisRef.javaClass.simpleName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: PreferenceModel, property: KProperty<*> ): String { val preference = getPreferenceForClass(thisRef) return preference.getString(property.name, default)!! }
  92. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( private val default: String ) : ReadWriteProperty<PreferenceModel,

    String> { private fun getPreferenceForClass(thisRef: PreferenceModel) = thisRef.context.getSharedPreferences( thisRef.javaClass.simpleName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: PreferenceModel, property: KProperty<*> ): String { val preference = getPreferenceForClass(thisRef) return preference.getString(property.name, default)!! }
  93. • ϓϩύςΟͷఆ͕͖ٛͬ͢Γͱ 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class UserSettings(override val context: Context) : PreferenceModel

    { var userName by PreferenceDelegate("") var iconUrl by PreferenceDelegate("") var coverPhoto by PreferenceDelegate("") }
  94. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( private val default: String ) : ReadWriteProperty<PreferenceModel,

    String> { private fun getPreferenceForClass(thisRef: PreferenceModel) = thisRef.context.getSharedPreferences( thisRef.javaClass.simpleName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: PreferenceModel, property: KProperty<*> ): String { val preference = getPreferenceForClass(thisRef) return preference.getString(property.name, default)!! }
  95. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( private val default: String ) : ReadWriteProperty<PreferenceModel,

    String> { private fun getPreferenceForClass(thisRef: PreferenceModel) = thisRef.context.getSharedPreferences( thisRef.javaClass.simpleName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: PreferenceModel, property: KProperty<*> ): String { val preference = getPreferenceForClass(thisRef) return preference.getString(property.name, default)!! }
  96. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( private val key: String, private val default:

    String ) : ReadWriteProperty<PreferenceModel, String> { private fun getPreferenceForClass(thisRef: PreferenceModel) = thisRef.context.getSharedPreferences( thisRef.javaClass.simpleName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: PreferenceModel, property: KProperty<*> ): String { val preference = getPreferenceForClass(thisRef) return preference.getString(key, default)!! }
  97. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( private val preference: SharedPreferences, private val key:

    String, private val default: String ) : ReadWriteProperty<PreferenceModel, String> { private fun getPreferenceForClass(thisRef: PreferenceModel) = thisRef.context.getSharedPreferences( thisRef.javaClass.simpleName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: PreferenceModel, property: KProperty<*> ): String { val preference = getPreferenceForClass(thisRef) return preference.getString(key, default)!! }
  98. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( private val preference: SharedPreferences, private val key:

    String, private val default: String ) : ReadWriteProperty<PreferenceModel, String> { private fun getPreferenceForClass(thisRef: PreferenceModel) = thisRef.context.getSharedPreferences( thisRef.javaClass.simpleName, Context.MODE_PRIVATE) override operator fun getValue( thisRef: PreferenceModel, property: KProperty<*> ): String { val preference = getPreferenceForClass(thisRef) return preference.getString(key, default)!! }
  99. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceDelegate( private val preference: SharedPreferences, private val key:

    String, private val default: String ) : ReadWriteProperty<PreferenceModel, String> { override operator fun getValue( thisRef: PreferenceModel, property: KProperty<*> ): String { return preference.getString(key, default)!! }
  100. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceLoader(private val default: String) { operator fun provideDelegate(

    thisRef: PreferenceModel, property: KProperty<*> ): ReadWriteProperty<PreferenceModel, String> { return PreferenceDelegate( thisRef.context.getSharedPreferences( thisRef.javaClass.simpleName, Context.MODE_PRIVATE ), property.name, default ) } }
  101. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceLoader(private val default: String) { operator fun provideDelegate(

    thisRef: PreferenceModel, property: KProperty<*> ): ReadWriteProperty<PreferenceModel, String> { return PreferenceDelegate( thisRef.context.getSharedPreferences( thisRef.javaClass.simpleName, Context.MODE_PRIVATE ), property.name, default ) } }
  102. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceLoader(private val default: String) { operator fun provideDelegate(

    thisRef: PreferenceModel, property: KProperty<*> ): ReadWriteProperty<PreferenceModel, String> { return PreferenceDelegate( thisRef.context.getSharedPreferences( thisRef.javaClass.simpleName, Context.MODE_PRIVATE ), property.name, default ) } }
  103. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceLoader(private val default: String) { operator fun provideDelegate(

    thisRef: PreferenceModel, property: KProperty<*> ): ReadWriteProperty<PreferenceModel, String> { return PreferenceDelegate( thisRef.context.getSharedPreferences( thisRef.javaClass.simpleName, Context.MODE_PRIVATE ), property.name, default ) } }
  104. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceLoader(private val default: String) { operator fun provideDelegate(

    thisRef: PreferenceModel, property: KProperty<*> ): ReadWriteProperty<PreferenceModel, String> { return PreferenceDelegate( thisRef.context.getSharedPreferences( thisRef.javaClass.simpleName, Context.MODE_PRIVATE ), property.name, default ) } }
  105. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceLoader(private val default: String) { operator fun provideDelegate(

    thisRef: PreferenceModel, property: KProperty<*> ): ReadWriteProperty<PreferenceModel, String> { return PreferenceDelegate( thisRef.context.getSharedPreferences( thisRef.javaClass.simpleName, Context.MODE_PRIVATE ), property.name, default ) } }
  106. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceLoader(private val default: String) { operator fun provideDelegate(

    thisRef: PreferenceModel, property: KProperty<*> ): ReadWriteProperty<PreferenceModel, String> { return PreferenceDelegate( thisRef.context.getSharedPreferences( thisRef.javaClass.simpleName, Context.MODE_PRIVATE ), property.name, default ) } }
  107. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceLoader(private val default: String) { operator fun provideDelegate(

    thisRef: PreferenceModel, property: KProperty<*> ): ReadWriteProperty<PreferenceModel, String> { return PreferenceDelegate( thisRef.context.getSharedPreferences( thisRef.javaClass.simpleName, Context.MODE_PRIVATE ), property.name, default ) } }
  108. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class PreferenceLoader(private val default: String) { operator fun provideDelegate(

    thisRef: PreferenceModel, property: KProperty<*> ): ReadWriteProperty<PreferenceModel, String> { return PreferenceDelegate( thisRef.context.getSharedPreferences( thisRef.javaClass.simpleName, Context.MODE_PRIVATE ), property.name, default ) } }
  109. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class UserSettings(override val context: Context) : PreferenceModel { var

    userName by PreferenceDelegate("") var iconUrl by PreferenceDelegate("") var coverPhoto by PreferenceDelegate("") }
  110. 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ class UserSettings(override val context: Context) : PreferenceModel { var

    userName by PreferenceLoader("") var iconUrl by PreferenceLoader("") var coverPhoto by PreferenceLoader("") }
  111. • ·ͱΊ • %FMFHBUFEQSPQFSUJFTͷجຊ͸HFU7BMVFTFU7BMVFΦϖϨʔλ • ϓϩύςΟͷ৘ใ͸QSPQFSUZύϥϝʔλ͔ΒࢀরՄೳ • UIJT3FGύϥϝʔλ͸ϓϩύςΟ͕એݴ͞Ε͍ͯΔΫϥεͷ Πϯελϯε •

    ಛఆͷܕʹ੍ݶ͢Δ͜ͱ΋Մೳ • όΠϯυ࣌ʹॲཧ΍ϓϩύςΟͷݕࠪ౳Λ ͍ͨ͠ͱ͖͸QSPWJEF%FMFHBUF 1SFGFSFODF%FMFHBUFΛ࣮૷͢Δ
  112. %FMFHBUFEQSPQFSUJFTͷJOMJOF࠷దԽ class DelegateSample { private val myProp$delegate = MyDelegate() var

    myProp: String get() = myProp$delegate.getValue(this, this::myProp) set(value: String) = myProp$delegate.setValue(this, this::myProp, value) }
  113. %FMFHBUFEQSPQFSUJFTͷJOMJOF࠷దԽ public final class DelegateSample { static final KProperty[] $$delegatedProperties

    = new KProperty[]{(KProperty)Reflection.mutableProperty1( new MutablePropertyReference1Impl( Reflection.getOrCreateKotlinClass(DelegateSample.class), "myProp", "getMyProp()Ljava/lang/String;"))}; @NotNull private final MyDelegate myProp$delegate = new MyDelegate(); @NotNull public final String getMyProp() { return this.myProp$delegate.getValue(this, $$delegatedProperties[0]); } public final void setMyProp(@NotNull String var1) { Intrinsics.checkParameterIsNotNull(var1, "<set-?>"); this.myProp$delegate.setValue(this, $$delegatedProperties[0], var1); } }
  114. %FMFHBUFEQSPQFSUJFTͷJOMJOF࠷దԽ public final class DelegateSample { static final KProperty[] $$delegatedProperties

    = new KProperty[]{(KProperty)Reflection.mutableProperty1( new MutablePropertyReference1Impl( Reflection.getOrCreateKotlinClass(DelegateSample.class), "myProp", "getMyProp()Ljava/lang/String;"))}; @NotNull private final MyDelegate myProp$delegate = new MyDelegate(); @NotNull public final String getMyProp() { return this.myProp$delegate.getValue(this, $$delegatedProperties[0]); } public final void setMyProp(@NotNull String var1) { Intrinsics.checkParameterIsNotNull(var1, "<set-?>"); this.myProp$delegate.setValue(this, $$delegatedProperties[0], var1); } }
  115. %FMFHBUFEQSPQFSUJFTͷJOMJOF࠷దԽ public final class DelegateSample { static final KProperty[] $$delegatedProperties

    = new KProperty[]{(KProperty)Reflection.mutableProperty1( new MutablePropertyReference1Impl( Reflection.getOrCreateKotlinClass(DelegateSample.class), "myProp", "getMyProp()Ljava/lang/String;"))}; @NotNull private final MyDelegate myProp$delegate = new MyDelegate(); @NotNull public final String getMyProp() { return this.myProp$delegate.getValue(this, $$delegatedProperties[0]); } public final void setMyProp(@NotNull String var1) { Intrinsics.checkParameterIsNotNull(var1, "<set-?>"); this.myProp$delegate.setValue(this, $$delegatedProperties[0], var1); } }
  116. • 'SPN,PUMJO ༧ఆ  • ҎԼͷ৚݅Λຬͨ͢%FMFHBUFEQSPQFSUZ͸ ,1SPQFSUZ഑ྻʹϝλσʔλΛ֨ೲ͠ͳ͍ • QSPWJEF%FMFHBUFHFU7BMVFTFU7BMVFશ͕ͯJOMJOFؔ਺ •

    JOMJOFؔ਺Ͱ,1SPQFSUZΛࢀর͍ͯ͠ͳ͍ • શͯͷ%FMFHBUFEQSPQFSUZ্͕هΛຬͨ͢ͱ͖ ,1SPQFSUZ഑ྻࣗମΛੜ੒͠ͳ͍ %FMFHBUFEQSPQFSUJFTͷJOMJOF࠷దԽ
  117. طଘΫϥεʹ%FMFHBUJPO͢Δ operator fun String.getValue(thisRef: Any?, property: KProperty<*>) = this.length class

    ExtensionDelegate { val length: Int by "delegate" fun main() { println(length) // 8 } }
  118. طଘΫϥεʹ%FMFHBUJPO͢Δ operator fun <T> SharedPreferences.getValue( thisRef: Any?, property: KProperty<*> ):

    T = this.all[property.name] as? T ?: throw IllegalArgumentException() operator fun <T> SharedPreferences.setValue( thisRef: Any?, property: KProperty<*>, value: T ) { val editor = edit() when (value) { is String -> editor.putString(property.name, value) is Boolean -> editor.putBoolean(property.name, value) // 省略 } editor.apply() }