This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-07-16
Channels
- # beginners (48)
- # cider (21)
- # clara (6)
- # cljdoc (3)
- # cljs-dev (11)
- # cljsrn (5)
- # clojure (30)
- # clojure-canada (1)
- # clojure-dusseldorf (2)
- # clojure-italy (10)
- # clojure-losangeles (2)
- # clojure-nl (4)
- # clojure-russia (8)
- # clojure-spain (18)
- # clojure-uk (39)
- # clojurescript (84)
- # core-async (17)
- # cursive (22)
- # data-science (27)
- # datomic (27)
- # docker (3)
- # editors (5)
- # emacs (2)
- # figwheel-main (18)
- # fulcro (54)
- # hoplon (3)
- # hyperfiddle (2)
- # immutant (4)
- # jobs (1)
- # jobs-discuss (1)
- # lein-figwheel (7)
- # leiningen (3)
- # lumo (1)
- # onyx (5)
- # re-frame (64)
- # reagent (5)
- # reitit (7)
- # ring-swagger (6)
- # shadow-cljs (118)
- # specter (23)
- # tools-deps (38)
Is that I should always close all channels after use them? or can just leave them to be collected by GC?
it really depends - most of the time one side of the communication or the other should close the channel so the other can stop looping (otherwise the gc can't happen as it's still waiting on the channel)
there's some smartness about gc'ing the channel and exiting the go block if there are no other references to the channel, but in my experience it's cleaner to explicitly close (and you should be checking for a closed channel in any code that reads or writes a channel btw.)
right - a good habit is to always check the return value of <!
and >!
and have some way to exit immediately if it returns nil (meaning closed)
by having a path that doesn't recur
or an if with a branch that exits the block
this is in general - sometimes if one channel is closed that means you are done with it and now you read another one - make sure this is relevant to you actual program structure
but what you want to avoid is an unchecked >!
or <!
so that you loop and get nils
know I found it's good to create channels at the beginning of a procedure, and close them when job is done. instead of create global channels.
also, it's good to write your code such that a channel is passed in
(that way your code is more flexible, the client can pass in a channel with a custom buffer and/or transducer to modify the behavior)