简体   繁体   中英

How to store a list of KFunction1 for kotlin?

Basically I want to create a router that can be used in the following way

router.register(someInstance::getData)
router.register(someInstance::submitData)

where both getData and submitData is an instance of

getParam : KFunction1<GetParam, GetResponse> 
submitData : KFunction1<SubmitParam, SubmitResponse>

where GetParam, GetResponse, SubmitParam, SubmitResponse is a class that implements respective interfaces (IParam, IResponse)

inside the register it would look like

val routeMap = mutableMapOf<String, KFunction1<IParam, IResponse>
fun register<P : IParam, R : IResponse>(func : KFunction1<P, R>) {
    routeMap["abc"] = func
}

only problem right now is that it seems that I can't access KFunction1 directly (so I cannot store them)

the idea is to basically create some sort of router that can register and call the registered functions later on. If anyone has any other idea on how to implement this, feel free to give suggestions

If you just want to register and call them, use the normal function types: (P) -> R , not KFunction1<P, R> .

But the real issue is that (P) -> R is not a subtype of (IParam) -> IResponse (and can't be because its invoke method doesn't accept any IParam ) and so can't be stored in routeMap .

How to fix this will depend on what you want to happen when types don't match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM