After a year of convincing myself that C# had done...
# announcements
g
After a year of convincing myself that C# had done covariance and contravariance right I was a little skeptical of "call site variance" declarations on types in kotlin. Well I've had my first run in with it: im trying to put a listener system on top of JNA, which means I have these obnoxious "Array<Any>" instances that represent a native-code function's arguments. I'd like to pass them to listeners but I need to make them immutable. I could of course allocate some objects, but then I'm allocating instances for unrelated purposes when native code is called, which seems pretty heavyweight. Enter call-site contravariance
val listener: (lib: Lib, methodName: String, args: Array<out Any>) -> Unit
this gets the compiler refusing to let callers modify the args list, and its got a really nice error message to boot: