Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"null channel should always block" #67

Open
summivox opened this issue Aug 9, 2015 · 2 comments
Open

"null channel should always block" #67

summivox opened this issue Aug 9, 2015 · 2 comments

Comments

@summivox
Copy link

summivox commented Aug 9, 2015

Rationale is explained excellently in this article: http://dave.cheney.net/2013/04/30/curious-channels

Since interface for channels are functions (instead of member of channel), could this possibly be implemented with a null check?

@ubolonton
Copy link
Contributor

Something like this should work (instead of "abusing" nil like that Go example):

var NULL_CHANNEL = {
  _put: function(value, handler) {
    return null;
  },

  _take: function(handler) {
    return null;
  },

  is_closed: function() {
    return false;
  },

  close: function() {
    throw new Error("Null channel cannot be closed.");
  }
}

However, waitMany could be implemented by removing the done channel from the list of channels to alts from.

function* waitMany() {
  var chans = Array.prototype.slice.call(arguments, 0);
  while (chans.length > 0) {
    var chan = (yield alts(chans)).channel;
    chans.splice(chans.indexOf(chan), 1);
  }
}

Is there any other better example where a null channel is needed?

@summivox
Copy link
Author

summivox commented Aug 9, 2015

From my limited knowledge of CSP, one example is guarded select block:

func maybe(b bool, c chan int) chan int {
    if !b {
        return nil
    }
    return c
}

select {
case <-maybe(val>0, p):
    val--
case <-v:
    val++
}

https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/ChPxr_h8kUM

Of course in javascript we can simply process the channel array before passing it to alts. It's just more convenient and consistent to have some sort of "always blocking" channel (I agree it doesn't have to be literally null).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants