-
Notifications
You must be signed in to change notification settings - Fork 624
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
Serializer for Nothing not found #614
Comments
I don't feel like However, what you want here is probably a |
I am of the perspective that @raulraja If you have a different perspective on this, please feel free to contribute it to this discussion. |
I think the issue here is not that sealed class Command<out A> {
data class Message(val value: String): Command<String>()
object Noop : Command<Nothing>()
}
Nothing itself does not need to be instantiated or serialized because the bottom type has 0 inhabitants.
I think all Serialization needs for this to work with is to ignore Revisiting a simple understood example which is object to this problem. One of the Arrow Data types. A simplified version: sealed class Option<out A> {
data class Some<out A>(val value: A): Option<A>()
object None : Option<Nothing>()
}
val someOne: Option<Int> = Some(1)
val noOne: Option<Int> = None In this example where val kindedUpperBound: Option<Any?> = Some(1)
val kindedLowerBound: Option<Int> = Some(1)
val kindedNothing: Option<Nothing> = Some(1) //does not compile because Option<Int> is not a subtype of Option<Nothing> In the case above since I believe you can support higher kinded class ImpossibleToSerializeOrInstantiate(val x: Nothing = TODO()) |
Perhaps a |
So it would be basically like this: object NotSerializable : KSerializer<Any> {
override val descriptor: SerialDescriptor = SerialClassDescImpl( "kotlin.Any")
override fun deserialize(decoder: Decoder): Any = throw SerializationException("Not serializable")
override fun serialize(encoder: Encoder, obj: Any) = throw SerializationException("Not serializable")
} Such serializer can be applied for types in generic arguments ( It even can be a default serializer for |
@sandwwraith The way I see it yes (except perhaps the return type for |
I've just started using kotlinx-serialization in a new project and this is basically the first issue I came across, with a compiler error that's not very helpful (location: "File is unknown"). Having a default serializer for |
I am trying to use kotlinx-serialization and I ran into this error.
Any help would be great! Thanks in advance :) |
@rubenquadros Assuming that the error you are getting refers to there not being a serializer for object NothingSerializer: KSerializer<Nothing> {
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("kotlin.Nothing") {}
override fun deserialize(decoder: Decoder): Nothing =
throw UnsupportedOperationException("Nothing does not have instances")
override fun serialize(encoder: Encoder, value: Nothing): Nothing =
throw UnsupportedOperationException("Nothing cannot be serialized")
} |
@pdvrieze I still get the same error. I am using kotlinx-serialization with ktor. Maybe I am doing something wrong. Here is my code runBlocking {
client.responsePipeline.intercept(HttpResponsePipeline.Transform) { (info, body) ->
val response = when {
context.response.status == HttpStatusCode.OK -> {
if (context.response.contentLength() == 0L) ApiResponse.SuccessNoBody
else ApiResponse.SuccessResponse(body)
}
else -> {
ApiResponse.ErrorNoBody(context.response.status.value)
}
}
proceedWith(HttpResponseContainer(info, response))
}
val response: ApiResponse<User, Nothing> = client.post("url")
} |
@rubenquadros You will need to actually make sure that you actually specify the serializer within ApiResponse. Something like: |
@pdvrieze Here is the stacktrace https://pastebin.com/pNYVKCyn |
@rubenquadros The problem here is that you somehow captured a |
Good point. I tried this first but it doesn't work for the JS target. Now the only workaround I can think of is creating a |
Hi everyone. I have tried both workaround solutions, using contextual serializers and mapping If you'd like to you can also depend the library from Maven Central. The code is still in prototype and I didn't add any tests yet so there is not much guarantee. However, we are already using it in our internal projects. |
@sandwwraith Any plans on moving this forward? |
Not having a Nothing implementation by default makes using kotlinx.serialisation impossible with any libraries using |
I guess you mean generic/parameterized types instead of higher-kinded types. If I am not wrong, higher-kinded types are not supported in Kotlin. |
My bad, yes. I meant parameterised types that use Nothing, which isn't uncommon. |
Describe the bug
Using
Nothing
in a generic type argument fails with an internal errorSerializer for element of type Nothing has not been found
.To Reproduce
This reproduces the problem when compiling:
Full compiler error: https://pastebin.com/FP6zGrDf
Expected behavior
Nothing
should be serializable.Environment
The text was updated successfully, but these errors were encountered: