https://kotlinlang.org logo
#coroutines
Title
# coroutines
o

otakusenpai

10/15/2018, 4:30 PM
Copy code
suspend fun addEvent(value: MsgType): Event {
    val eventChannel = Channel<Event>()
    var event: Event? = null

    log("joining", logMessages)
    if (value == MsgType.ping) {
        log("ping", logMessages)
        eventChannel.send(PingEvent(value) as Event)
    }
    else if (value == MsgType.join) {
        log("join", logMessages)
        eventChannel.send(JoinEvent(value) as Event)
    }
    else if (value == MsgType.normal) {
        log("normal", logMessages)
        eventChannel.send(NormalEvent(value) as Event)
    }
    else if (value == MsgType.privmsg) {
        log("privmsg", logMessages)
        eventChannel.send(PrivMsgEvent(value) as Event)
    }
    else if (value == MsgType.mode) {
        log("mode", logMessages)
        eventChannel.send(ModeEvent(value) as Event)
    }
    else if (value == MsgType.notice) {
        log("notice", logMessages)
        eventChannel.send(NoticeEvent(value) as Event)
    }
    else if (value == MsgType.special) {
        log("special", logMessages)
        eventChannel.send(SpecialEvent(value) as Event)
    }
    else if (value == MsgType.namelist) {
        log("namelist", logMessages)
        eventChannel.send(NameListEvent(value) as Event)
    }
    else if (value == MsgType.topic) {
        log("topic", logMessages)
        eventChannel.send(TopicEvent(value) as Event)
    }
    else if (value == MsgType.topic_whotime) {
        log("whotime", logMessages)
        eventChannel.send(TopicWhoTimeEvent(value) as Event)
    }
    else if (value == MsgType.quit) {
        log("quit", logMessages)
        eventChannel.send(QuitEvent(value) as Event)
    }
    else if (value == MsgType.who) {
        log("who", logMessages)
        eventChannel.send(WhoEvent(value) as Event)
    }
    else if (value == MsgType.end_of_who) {
        log("end of who", logMessages)
        eventChannel.send(EndOfWhoEvent(value) as Event)
    }

    log("receiving", logMessages)
    event = eventChannel.receive()

    log("received", logMessages)
    if(event == null)
        throw AghoraException("Error: In addEvent(value: MsgType): Event in " +
        " com.github.otakusenpai.aghora.irc: No implementation of MsgType ${value.toString()}")

    log("returning", logMessages)
    return event
}
Can anyone explain why this code isnt returning the value from the Channel ?
Copy code
[main] Connected!
[main] connected, launching
[main] inside if
[main] done running coroutine
[main] loop 1
[main] I'm now in Connect!
[main] Entering loop...
[main] Received data: :<http://verne.freenode.net|verne.freenode.net> NOTICE * :*** Looking up your hostname...
[main] I'm now in segragate!
[main] Inside if statement
[main] Loop 0
[main] In ctor
[main] First
[main] Second
[main] Third
[main] Fourth
[main] Assign
[main] if
[main] eventHandler
[main] joining
[main] notice
These are some helping printlines that i made to debug the source of the problem
e

enleur

10/15/2018, 4:33 PM
wow.. replace it with when expression
5
o

otakusenpai

10/15/2018, 4:38 PM
yeah i also tried tht
i just added the log lines for debugging
its not calling these 2 lines
Copy code
log("receiving", logMessages)
event = eventChannel.receive()
Are there any rules as to casting in a channel ?
c

codyoss

10/15/2018, 5:52 PM
val eventChannel = Channel<Event>(1)
fix the issue?
g

gildor

10/15/2018, 11:23 PM
You also can have multiple expressions per when case, just use curly braces
4 Views