Lazy evaluation in non-selected select cases

153 views
Skip to first unread message

Josh Bleecher Snyder

unread,
Jun 25, 2013, 8:54:26 PM6/25/13
to golang-nuts
One of the best things about learning Go has been that almost every time I ask "why doesn't it do <x> the way I expect?", the answer has been very instructive.

In that spirit, why aren't channel inputs in non-selected select cases evaluated lazily?

See:


It seems to me that, in the second example, because nilChannel isn't selected in the case statement, there is no need to evaluate the proposed send value. The sample code is silly, but this would e.g. obviate the need for the variable first in http://talks.golang.org/2013/advconc.slide#30.

Cheers,
Josh


Jesse McNelis

unread,
Jun 25, 2013, 9:13:57 PM6/25/13
to Josh Bleecher Snyder, golang-nuts
On Wed, Jun 26, 2013 at 10:54 AM, Josh Bleecher Snyder <josh...@gmail.com> wrote:
One of the best things about learning Go has been that almost every time I ask "why doesn't it do <x> the way I expect?", the answer has been very instructive.

In that spirit, why aren't channel inputs in non-selected select cases evaluated lazily?

Once a choice is made the communication has begun and any time spent computing the channel input value is time spent blocking the channel.
It would be silly to block a channel while you did some large computation, it's far more reasonable to do the computation first.

See:


It seems to me that, in the second example, because nilChannel isn't selected in the case statement, there is no need to evaluate the proposed send value. The sample code is silly, but this would e.g. obviate the need for the variable first in http://talks.golang.org/2013/advconc.slide#30.

Cheers,
Josh


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
=====================
http://jessta.id.au

Josh Bleecher Snyder

unread,
Jun 25, 2013, 9:46:26 PM6/25/13
to Jesse McNelis, golang-nuts
>> One of the best things about learning Go has been that almost every time I ask "why doesn't it do <x> the way I expect?", the answer has been very instructive.
>>
>> In that spirit, why aren't channel inputs in non-selected select cases evaluated lazily?
>
>
> Once a choice is made the communication has begun and any time spent computing the channel input value is time spent blocking the channel.
> It would be silly to block a channel while you did some large computation, it's far more reasonable to do the computation first.

Thanks! That makes a lot of sense.

-josh

Reply all
Reply to author
Forward
0 new messages