question about inheritance: - Using an abstract cl...
# announcements
question about inheritance: • Using an abstract class or interface i can ensure that every child class has certain functions and fields. • Companion objects can implement interfaces Is there also a way to ensure that child classes have a companion object that implements a certain interface?
I would be very surprised if that was the case.
Companion objects aren't inherited as they're part of the Class definition, and not it's instance. Odds are there's a better way to achieve what you're trying to achieve, especially since companion function references aren't don't polymorphically, but rather statically (so compile time). I don't think I explained myself very well, but maybe it will help. Perhaps describe what you're trying to achieve, and we can propose an alternative approach?
Code example is possibly the best explanation 😉 I can't look at it right now as corporate firewall blocks Github... Royal pain in the ...
wtf, what kind of corporation do you work at
It's a long story. Some code was uploaded to Github that shouldn't have been. Usual corporate response is to lock it down. Not that that stops the real problem. It's being revisited, but until then... you truly learn how much documentation for us is hosted on Github. Breaks homebrew etc.
I think i know what you're searching for, but it's not possible atm. Typeclasses would help, take a look at keep-87. The only current option is to implement the interfaces in your objects or companion objects and take them as an additional input in your functions. Otherwise you would need to associate the companion instances somehow with your opcode instances... Making the static object accessible as an instance property effectively, although theoretically, one could implement it without a companion as Well then. If i understand it correctly, your oocode class is under your control and can be a sealed class, so using companions would work in when branches regardless of any interface nonetheless... doesn't enforce presence of a companion, but you will notice in the when branch when you want to use it, so why does it matter :)
What kind of repeating are you trying to get rid of? To me it's not entirely clear
Well a thing you maybe sort of could do (assuming the Handler and the other class implement an interface) is:
Copy code
val handlerAndEventPerOpcode = mapOf(
  Opcode.CreatureUpdate to (CreatureUpdateHandler to CreatureUpdate),
  Opcode.CreatureAction to (CreatureActionHandler to CreatureAction),
val (handler, event) = handlerAndEventPerOpcode[opcode]
handler.handlePacket(event.readFrom(reader), player)
But not sure if that would be better, and you also gotta figure something out for the ChatMessage one