https://kotlinlang.org logo
#announcements
Title
# announcements
l

LastExceed

01/21/2020, 12:09 PM
is there any proper way to create abstract factory functions to force their implementation? I have an abstract class
Packet
(for networking) that needs a
serialize
and
deserialize
function. the former is easy, just a regular abstract function with a
ByteWriteChannel
param to write the packet into. the latter however needs to be either a top-level extension function for
ByteReadChannel
or in the companion object, neither of which allows the
abstract
keyword (obviously). how do I ensure that each
Packet
class implements a
deserialize
factory function ?
n

Nikky

01/21/2020, 11:24 PM
how would this even be used? since you do not have the instance of the packet yet .. how would it know of what type it is and which
deserialize
to call? i would opt for a exhaustive
when
with some enum or making Packets a sealed class that way i should not overlook or miss any
if you use kotlinx-serialization then polymoprhic serializers for sealed classes are also a cool option, but it might need some manual tweaking to get this working for a
ByteReadChannel
, assuming your packets are not in a serialization supported format