Bernhard
08/13/2024, 4:59 PMturansky
08/13/2024, 7:09 PMturansky
08/13/2024, 7:10 PMBernhard
08/13/2024, 7:10 PMBernhard
08/13/2024, 7:10 PMBernhard
08/13/2024, 7:11 PMBernhard
08/13/2024, 7:13 PMBernhard
08/13/2024, 7:13 PMBernhard
08/13/2024, 8:52 PMvar template = Handlebars.compile("Handlebars <b>{{doesWhat}}</b>");
template({ doesWhat: "rocks!" })
You might type the template function as
external fun template(context: ReadOnlyRecord<String, Any?>): String
But since you know which variables are used inside the template, you might want to define the parameter as
@JsPlainObject
external interface Context {
val doesWhat: String
}
template(Context(doesWhat="rocks")) // compile error
I think the solution is to generate a method/extension function called .toRecord()
template(Context(doesWhat="rocks").toRecord())
Edoardo Luppi
08/14/2024, 9:17 AMRecord
yourself I suppose.
inline fun <K : Any, V> Any.toRecord(): Record<K, V> = unsafeCast<Record<K, V>>()
You could add whatever check to validate this
Bernhard
08/14/2024, 9:18 AMBernhard
08/14/2024, 9:18 AMEdoardo Luppi
08/14/2024, 9:19 AMBernhard
08/14/2024, 9:19 AMBernhard
08/14/2024, 9:20 AMEdoardo Luppi
08/14/2024, 9:23 AMturansky
08/14/2024, 12:04 PMI think the main issue here is that js-plain-objects is in Kotlin core whereas record is in a community repoIt's not a blocker with K2
turansky
08/14/2024, 12:06 PMBut I think the idea to have a plain object implement Record<K, V> is valid.
ReadonlyRecord
- fine
Record
- isn't fineBernhard
08/14/2024, 12:36 PM