```class ImageRecognizerImpl : ImageRecognizer { ...
# kotlinx-rpc
u
Copy code
class ImageRecognizerImpl : ImageRecognizer {
    override suspend fun recognize(image: Image): RecogniseResult {
        val byte = image.data[0].toInt()

        return RecogniseResult { <------------------
            category = if (byte == 0) 0 else 1
        }
    }
}
Why not the usual constructor shape?
m
I guess because platform independent implementations require Interfaces and this shape is replacing the builder pattern that you might know from official grpc java and kotlin implementations.
a
Same reasoning as in grpc-kotlin Fields in proto messages are designed to be unordered, adding a new one should not change the order in which values are assigned in the constructor. If we would have enforced named params then this wouldn't be needed
u
im not sure if I follow, isnt this a detail of a some internal serializer?
a
No, it is how you use generated Api. If you had proto fields
int32 a = 1
and
int32 b = 2
, and then decided to add
int32 c = 3
in between first two for any reason - your existing code will have a silent logic error without named parameter enforcement ( call like
MyMessage(1,2)
will treat 2 as
b
fir the old version and as
c
for the new one without compilation or runtime error. Other reason for dsl, and that protobuf has a notion of explicit presence, which is different from default values. You can't express this with a Kotlin constructor, default value 1 is not distinguishable from the explicitly set value 1 without a custom setter in a DSL
u
Okay so this is means of the framework protecting me from backwards incompatibility?
a
Yes, and enforcing protobuf spec compliance without surprises
u
but if it's just a numerical name, isn't there some compiler magic that could take it into account when serializing? sounds to me lot like
@SerialName("1") a: Int
in json deserialization Wire seems to have ctors
WhiteboardUpdate(update_points = UpdatePoints(points)
https://github.com/square/wire/blob/master/samples/wire-grpc-sample/server/src/main/java/com/squareup/wire/whiteboard/WhiteboardGrpcAction.kt
a
I don't know how compliant Wire to the spec, we concluded that this is not possible during the development, for the same reasons Google's grpc-kotlin concluded the same
u
they're in the #squarelibraries btw
a
They might not anymore, afaik, Square sunset most OSS projects. There are migrated libs, but Wire is not one of them Square itself might maintain it, but also might not (no official statement) https://jakewharton.com/the-lysine-contingency/ https://www.reddit.com/r/Kotlin/comments/1u8cotp/comment/oshpfds/?force-legacy-sct=1
u
yea it's sad, but I only mention them maybe they have insight
a
Btw, you can read about the explicit presence problem here: protobuf.dev/design-decisions/nullable-getters-setters tldr;
Copy code
class MyMessage(val a: Int = 0)

// In protobuf this
MyMessage(a = 0)

// is not the same as this
MyMessage()

// but Kotlin constructors can't express this, DSLs can using custom setters
c
Right. But proto3 doesn't have default values, so I don't see the ambiguity? It's a pain point that proto3 doesn't differentiate between
null
and an empty list.
Does proto3 have explicit presence?
a
But also, proto3 is not the standard, nor is it the latest version. Latest is editions 2024
👀 1
today i learned 1
It's a pain point that proto3 doesn't differentiate between
null
and an empty list.
That's because they don't have nulls, and they think explicit presense is a better notion than nulls
I mean, we decided to sit on both chairs! github.com/Kotlin/kotlinx-rpc/pull/644
c
IMO it's a pain to have to write
Copy code
repository.findUsers(
    ids = request.ids.takeIf { request.hasIds() }
)
because we constantly forget it and it leads to bugs. I haven't tested krpc yet though so maybe that's less of an issue here (because in the DB,
null
means 'no criteria, return all data' vs
emptyList()
which means 'one of none, return no data', which leads to security issues where users have access to information they shouldn't)
a
Wait, why
orEmpty()
then?
c
sorry, I got it wrong
✅ 1
a
Hm, this indeed maps poorly
But I still don't get how it works. gRPC doesn't have a presence for repeated fields There is no way to distinguish empty list from a value not set without an extra field
oooh, sorry
c
Yes, meaning we can't call the endpoint with an actual empty list even if we wanted to.
a
Got it, you don't need an explicit empty list
well, shit ahaha
c
Well, in the case of a filter, an explicit empty list would return no value at all, so it would be a no-op anyway and we would want to short-circuit that on the caller side. In the case of a mutation, an explicit empty list could be useful. That forces us to decide for every endpoint if it uses
null
or
emptyList()
, because we can't have a value that could be either.
a
yes fair do you have any ideas how to make this better, but stay compliant to the protobuf spec?
😅 1
🌚
c
Well, if there was explicit presence, then explicit presence but no values would be an empty list, and no explicit presence would be
null
You could add an auto-generated boolean neighbor field to emulate explicit presence, but any other code generator wouldn't know about it 😐
a
Yep, that's a general problem, and the reason why we don't do anything fancy with our implementation