I want to use the Java Preferences API to store some data for my application. Since I'm writing the application in Kotlin, I would like to take advantage of delegated properties to get and set my preferences.
My current setup is like this
object Pref {
val systemPref by lazy { Preferences.systemNodeForPackage(App::class.java) }
val userPref by lazy { Preferences.userNodeForPackage(App::class.java) }
// example preference
const val SETTING = "setting"
var setting: Int
get() = userPref.getInt(SETTING, -1)
set(value) = userPref.putInt(SETTING, value)
}
But I would like to have something like this to declare the preferences
var setting: Int by preference(userPref, "setting", -1)
The API has different methods for accessing preferences of different data types. I would like to use those, but it complicates things for me. Is there a way to have such a delegate which would work with any data type and also wouldn't convert everything to and from strings when talking to the library?