If somebody is online to help :sweat_smile::grin::...
# announcements
a
If somebody is online to help 😅😁: https://stackoverflow.com/q/64924393/11377112
n
out of curiosity what if you drop the
inline
?
also I'd double-check you're not importing a different
EventAdapter
a
Nope, nothing happens (same error). Yes EventAdapter is correctly imported.
Though using star-projections, yield the upper bount i.e.
Event
.
And call site it is inferred as
MessageCreateEvent
n
The things you're returning from your when
Have no common base class
a
Base interface?
n
There is no base interface
There's a parameterized family of Base interfaces
They implement different members of that family
a
🤔
Unchecked cast is the only way to handle such cases?
n
You could add an Event Adapter base class
Which event adapter inherits from
This class is not generic
a
Didn't understood well
n
And has val event: Event
a
That's what I don't want to do
n
Why?
a
MessageCreateEvent.toAdapter should return adapter whose event property should be MessageCreateEvent
n
Then use overloading
a
And inline classes won't allow inheritence iirc
n
Not when
a
Then how'd I handle nullable cases (i.e. when no adapters are available for some Event types)?
Well I didn't understood why
KordMessageCreateAdapter(this)
is not a
KordEventAdapter<MessageCreateEvent>
?
n
I'm not sure why your want that, given the way you're setup here
a
Though using the star projections, it is inferred as
KordEventAdapter<Event>
even though it is not covariant
n
Like I said, you are returning two types from your when that have no common base
Maybe you're missing an in or out annotation
a
given the way you're setup here
I'm making adapters for different types because they don't have common methods but does contain some similarities
n
Without covariance though, what do you think the common base of the two types in the when is?
MessageCreateAdapter and MessageUpdateAdapter have no common base so there's nothing for that function to return
That's why it doesn't compile, but it also leaves open the question of what useful purpose that function could serve
a
MessageCreateAdapter
is
EventAdapter<MessageCreateEvent>
. And
MessageUpdateAdapter
is
EventAdapter<MessageUpdateEvent>
. So both are
EventAdapter<E>
whichever event came in?
Like mutableListOf(1, 2, 3) returns MutableList<Int> when ints come, and mutableListOf(1.2, 3.5) returns MutableList<Double>
Int comes -> MutableList<Int> Double comes -> MutableList<Double> Though both are invariant
n
Yes but that's a generic function, it returns different types based on the type argument
You have a when block
a
Yes, so do I not return different types based on the event came in?
n
You can't do that
In a statically typed language, expressions have to have types that can be determined statically
That when block has to have a type that's determined statically
val x = if (true) 1 else 1.0
What's the type of x?
a
👍 Got it, I can cast them to respective type at runtime as I'm checking those things at runtime with when
n
Yes. But you should rethink design a bit, like I said, because there is no common base, it's hard to imagine how that function can do something useful
One solution would be to give a common interface like I said
a
Comment on SO helped, and your argument was correct but I wasn't truely understanding. Now got it:)
it's hard to imagine how that function can do something useful
It does, it is used for extraction of things from differnt events which does not have same datastructure
🙂
n
Copy code
interface EventAdapterBase {
    val event: Event
    // ...
}
interface EventAdapter<E : Event> : EventAdapterBase {
    override val event: E
    // ...
}
Yes but functions have to have a singular return type for a given set of type arguments, even if the data structure is not the same, you have to establish something in common
Otherwise the function can only return Any and you can't do anything more interesting with the result than with the input
Anyway I need to sleep but I can explain more tomorrow if you're around. These patterns show up a lot in C++which is my background so I'm pretty familiar with them
👍 1
a
Thanks!