1

Context

I'm building a Flutter application where I need to use some 3rd party library that only works with the native platform. For this, I'm using en EventChannel to get recurring information.

As Flutter and native code can't ensure the types between their communication (the communication methods explicitly define dynamic for the response), I'm trying to add a wrapper of the EventChannel.StreamHandler class, where I could limit the types of that class to match only the ones allowed by flutter.

What I've done so far?

abstract class EventChannelHandler<T> : EventChannel.StreamHandler {
    protected abstract val childClassname: String
    
    private var eventSink: EventChannel.EventSink? = null
    
    override fun onListen(arguments: Any?, eventSink: EventChannel.EventSink?) {
        this.eventSink = eventSink
    }
    
    override fun onCancel(arguments: Any?) {
        eventSink = null
    }
    
    fun add(value: Option<T>) {
        val flutterValue = value.getOrElse { null }
        
        eventSink?.success(flutterValue)
    }
}

This is a base class for some data, and those data would use the above class the following way:

class SpecificData1EventChannelHandler : EventChannelHandler<Int>()
class SpecificData2EventChannelHandler : EventChannelHandler<Double>()
class SpecificData3EventChannelHandler : EventChannelHandler<String>()

Problem

As we can see above, using generics, it allows any kind of type, which doesn't solve my problem.

Final Question

How can I ensure my wrapper class only accepts a specific set of predefined types so that my add function only handle/deliver types that Flutter can understand?

OBS: I'm aware of Pigeon, but I won't use it since it's not stable.

Fernando Santos
  • 392
  • 4
  • 19
  • 1
    It's in the documentation. https://kotlinlang.org/docs/reference/generics.html#upper-bounds But it it has to satisfy all bounds, not any. Otherwise, using generics is nonsensical. – Tenfour04 Sep 13 '20 at 16:35
  • Yes! I saw this, but this would be A && B && C as far as I understood. I needed the OR operator – Fernando Santos Sep 13 '20 at 17:03
  • It doesn't make sense to have an OR operator for generics. How would you call a function on the generic reference if the function is only available on one of the types? You would have to cast, at which point using OR types would be useless and you could accomplish the exact same thing by specifying the common supertype. – Tenfour04 Sep 13 '20 at 17:51
  • Hm... You're fully correct. I guess I've made a bad question. What I need is a way to check if a variable is one of a set of limited types. Something like an Any type, but that allows only A, B, or C types... I'll update the question. My bad! – Fernando Santos Sep 13 '20 at 18:08
  • 1
    There is no feature for that in Kotlin. – Tenfour04 Sep 13 '20 at 18:15
  • You can accomplish something similar with function overloads. – Tenfour04 Sep 13 '20 at 18:40
  • @Tenfour04 I've updated the question with much more info to try to don't let things out. – Fernando Santos Sep 13 '20 at 21:37

0 Answers0