Erik Dreyer
09/27/2022, 7:05 PMe
in the main()
function determines the return type of the dispatch()
function.
The error I’m getting is:
Type mismatch.
Required:
Result<E>
Found:
Result<ExampleEvent>
Erik Dreyer
09/27/2022, 7:06 PMBence Erős
09/27/2022, 7:22 PMBence Erős
09/27/2022, 7:23 PMval event: E = ExampleEvent("tada!")
?dave
09/27/2022, 7:29 PMdave
09/27/2022, 7:29 PMErik Dreyer
09/27/2022, 7:33 PMEvent
Erik Dreyer
09/27/2022, 7:34 PMI think the problem may be because Result is invariantThis is probably the root issue
Erik Dreyer
09/27/2022, 7:34 PMErik Dreyer
09/27/2022, 7:36 PMResult
looks like it’s setup for covariance: Result<out T>
dave
09/27/2022, 7:37 PMdave
09/27/2022, 7:41 PM<E: ExampleEvent>
it is still broken because ExampleEvent cannot be an E.Erik Dreyer
09/27/2022, 7:42 PMErik Dreyer
09/27/2022, 7:44 PMfun <E: Event> dispatch(...): Result<E>
such that the type contained by Result was a subclass of Event
and specified by the generic parameter E
dave
09/27/2022, 7:54 PMI thought E was bound to ExampleEvent because ExampleEvent “is a” EventThe compiler doesn't know about ExampleEvent in the function signature - there is only E and Event at that point.
such that the type contained by Result was a subclass ofyes - it is - but nowhere in that is ExampleEvent 🙂and specified by the generic parameterEvent
E
dave
09/27/2022, 7:54 PMErik Dreyer
09/27/2022, 7:55 PMEvent
that would be enough, given that ExampleEvent is a subtypedave
09/27/2022, 7:59 PMErik Dreyer
09/27/2022, 8:01 PM<E : Event>
, they don’t play nicely together. You’d think they would.Erik Dreyer
09/27/2022, 8:03 PMRequired:
Result<E>
Found:
Result<ExampleEvent>
I thought it would know that ExampleEvent “is a” Event, thus satisfying that boundErik Dreyer
09/27/2022, 8:04 PMErik Dreyer
09/27/2022, 8:10 PMResultEvent
because ExampleEvent
satisfies the upper bound of the generic.
Unless I specify the type…Erik Dreyer
09/27/2022, 8:11 PMval re = ResultEvent(ExampleEvent("tada!"))
Erik Dreyer
09/27/2022, 8:11 PMErik Dreyer
09/27/2022, 8:13 PMval re = ResultEvent(ExampleEvent("tada!")) as ResultEvent<E>
Then this
println(testingCovariance<ExampleEvent>())
prints this
ResultEvent(e=ExampleEvent(msg=tada!))
Erik Dreyer
09/27/2022, 8:14 PMAlexis Manin
09/27/2022, 11:47 PMfun dispatchCreate() : CreateEvent
fun dispatchUpdate() : UpdateEvent
fun dispatchDelete() : DeleteEvent
The first solution is heavier for users, the second solution is more work for the author of the API.
Or you can just use reified types, if runtime safety is enough.Erik Dreyer
09/28/2022, 3:01 AM