kvision icon indicating copy to clipboard operation
kvision copied to clipboard

Services with nullable return types

Open nea89o opened this issue 4 years ago • 1 comments

I wanted to create a Service with a nullable return type recently, which, (as the docs point out) doesn't work. So i used a wrapper like this:

@Serializable
class MyOptional<T>(
        val value: T?
) // Can't use java optionals

Now i thought: would it be possible to automatically generate code for wrapper functions that utilize a wrapper like this one, but internally call methods with normal nullable return type.

Would this be an appreciated contribution, and if so has anyone any code pointers on where to start?

nea89o avatar Jun 08 '20 20:06 nea89o

Hello and thx for your feedback :-)

It's quite difficult to answer your question and I'm not even sure I'm able to explain. KVision server side interfaces code is based on inline functions and reified type parameters. It is required because at the deepest level kotlinx.serialization library is used to serialize and deserialize generic data. And almost all serialization functions are defined for T : Any types, which excludes nullable types. And because of the nature of inline functions, that exclusion is popping to the top and makes it impossible to use nullable types at the Service level. With the help of casting and some "dirty hacks" I've implemented nullability for parameters of methods. But as I don't have control over the returned data, there is no way to do the same for deserialization of the results. And that's the reason of the current limitation.

But it's not the end of the story, because where was no compiler plugin when all of this was implemented. Now, with the help of compiler plugin it should be possible to generate some kind of a wrapper. But I'm afraid it would require much work. Currently the plugin operates on the very simple level - it basically works with strings manipulation. I think it would be required to deeply analyze Kotlin code inside the plugin code to generate working wrapper. Unfortunately the compiler plugin API is not documented so it would require a lot of work, trials and errors. I would really appreciate the contribution, but I will definitely understand if you find that the effect is not worth the effort :-)

rjaros avatar Jun 08 '20 22:06 rjaros