I have the following data class in my common code:...
# multiplatform
j
I have the following data class in my common code:
Copy code
data class SomeModel(val createdAt: Instant)
In JS and Darwin targets, do you any recommandation to provide native dates for the
createdAt
property? Details in thread ➡️ 🧵
I was thinking about extension properties/functions:
Copy code
// JS
@JsExport fun SomeModel.createdAtJS() = createdAt.toJSDate()

// Darwin
val SomeModel.createdAtNS get() = createdAt.toNSDate()
However : • The JS target doesn’t support extension properties, and the extension function is a bit deceptive being a simple function with the receiver passed as a parameter:
createdAtJS(myModel)
• The model still accepts an
Instant
type in its constructor, thus I can’t instantiate it from the JS/Darwin targets since I can’t provide an instance of this type. I could create functions to make the conversion tho. • The original
createdAt
property is still available and can be read from all targets. How do you solve this in your own codebase?
s
Why not use KotlinX Datetime? It covers
Instant
. https://github.com/Kotlin/kotlinx-datetime
j
Actually, I use KotlinX DateTime! Sorry, I did not precise it 😅
However,
Instant
from this lib is not JS exportable
but at least it provides those nice extension functions
toJSDate()
and
toNSDate()
I’m juste wondering how to use them properly in my codebase
s
Right, I would create smart-constructors for them. Afaik top-level non-extension functions are the best for that if you want to call them from everywhere.
j
For the constructor, maybe on my property, instead of using
Instant
, I could use
String
to accept ISO8601 strings, that way all the targets can instantiate the data class
Yeah, you’re probably right 🙂