On Wed, Jan 24, 2018 at 9:12 PM, roger peppe <rogpe...@gmail.com> wrote:

> Why? Consider this code. When c is first evaluated, it's ok to send
> on, so it would
> evaluate f, but f causes c to be not ready, so f has been evaluated even
> though
> c cannot be sent on.
>
>     package main
>
>     import (
>         "log"
>         "time"
>     )
>
>     func main() {
>         c := make(chan int, 1)
>         f := func() int {
>             c <- 1
>             return 0
>         }
>         select {
>         case c <- f():
>         case <-time.After(time.Second):
>             log.Printf("could not send value")
>         }
>     }
>

This is a pretty cool counter-example. You can even take it one step further

c := make(chan int, 1)
f := func() int {
    c = nil
    return 0
}
select {
case c <- f():
}

whoops.


> On 24 January 2018 at 19:59, dc0d <kaveh.shahbaz...@gmail.com> wrote:
> > It is as described in the spec. Regarding the specs it's correct. But
> that's
> > not the reason.
> >
> > The original question is about the "why".
> >
> > Why is that so? This behavior was unexpected.
> >
> > On Wednesday, January 24, 2018 at 11:20:01 PM UTC+3:30, Axel Wagner
> wrote:
> >>>
> >>> Both the channel and the value expression are evaluated before
> >>> communication begins. Communication blocks until the send can proceed.
> A
> >>> send on an unbuffered channel can proceed if a receiver is ready. A
> send on
> >>> a buffered channel can proceed if there is room in the buffer. A send
> on a
> >>> closed channel proceeds by causing a run-time panic. A send on a nil
> channel
> >>> blocks forever.
> >>
> >>
> >> https://golang.org/ref/spec#Send_statements
> >>
> >> So, yes.
> >>
> >> On Wed, Jan 24, 2018 at 8:43 PM, dc0d <kaveh.sh...@gmail.com> wrote:
> >>>>
> >>>> If the channel is nil, there's nothing to lock. But also, there's no
> >>>> sending happening.
> >>>
> >>>
> >>> So first the payload is computed and then the nil check happens?
> >>>
> >>> --
> >>> 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/d/optout.
> >>
> >>
> > --
> > 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+unsubscr...@googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
>
> --
> 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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to