Why can't I explicitly return void from a method?

前端 未结 14 2322
萌比男神i
萌比男神i 2021-01-01 08:23
void run() {
    ...
    if (done) return cancel();
    ...
}

where cancel() return void. This won\'t compile... and I ca

14条回答
  •  难免孤独
    2021-01-01 08:54

    Interesting idea. The main issue is the language spec, which defines a return statement as being composed of return . A void method is not an expression, so the construct isn't permitted.

    You've found that you can replicate the functionality by executing the void method and then returning, so there's no real reason to allow it.

提交回复
热议问题