I have a data structure that represents a pointer ...
# naming
c
I have a data structure that represents a pointer to some remote data (= will need to be deserialized after it got queried).
Copy code
interface Ref<T> {
    …
}
In some situations, the type
T
isn't the one you want, in a way that the compiler cannot verify. For example if you have very arbitrary data, you may want to just treat it as a
Ref<JsonObject>
. How should I name the function that changes the type? The name should be clear that the correctness is not verified (it can cause future deserialization to fail, it's a type-safety bypass): •
unsafeCast
•
reinterpret
• something else?
m
c
No, it's a custom function and not a real cast.
m
Then don't call it cast-something
c
for the user, it's the same. It looks like
Copy code
val a: Ref<A> = …

val b: Ref<B> = a.xxx<B>()
(compare to
a.downcast<B>()
which gives
B
)
m
If there is "cast" inside, it's strongly connoted. There are already a bunch of things in
kotlin-stdlib
(not public):
Copy code
inline fun <reified T : Any> Any?.safeAs(): T? = this as? T
inline fun <reified T : Any> Any?.cast(): T = this as T
inline fun <reified T : Any> Any?.assertedCast(message: () -> String): T = this as? T ?: throw AssertionError(message())
If the function is framework specific, I'd stay clear of "cast"
b
convert
➕ 2
y
If it affects deserialization, then maybe
deserializeAs
? Maybe it doesn't even have to be tied to deserialization at all, so something like
map
could do the trick, where inside you'd spell out the transformation logic (which can be as simple as using kotlinx-serialization to turn JSON into a data class).
c
It doesn't deserialize itself, serialization is affected because it uses T to decide which serializer to use
convert
is already a function of this class and it actually transforms the data 😅
b
then...
transform
😄
y
I like
reinterpret
then
c
To me
transform
conveys that the data is changed in some way, whereas this function really doesn't, it just changes the Kotlin type for it
b
maybe something about
facet
? not sure what exactly
.fromACertainPointOfView()
😄
m
Call it
the<T>()
gradle intensifies
🤣 3
y
spillThe<T>()
for the Gen Z appeal
😂 5
🍵 1
p
.transmute<T>()
or
.transmogrify<T>()
have my vote.
😮 1
a
.asRef<T>
👀 1