Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Services with nullable return types #158

Open
nea89o opened this issue Jun 8, 2020 · 1 comment
Open

Services with nullable return types #158

nea89o opened this issue Jun 8, 2020 · 1 comment

Comments

@nea89o
Copy link

nea89o commented Jun 8, 2020

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?

@rjaros
Copy link
Owner

rjaros commented Jun 8, 2020

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 :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants