Why is it `close(::Channel)` instead of `close!(::Channel)`?


Out of curiosity, why does closing a channel not have a ! in its name?

1 Like

There is no explicit data structure being mutated. Yes, I/O operations change the state of the I/O stream but that’s inherent to the way they function. It could be viewed the other way but then all I/O operations would end in ! which seems unnecessary.


True, println!(42) would be odd. Though it’s actually doing quite a similar thing compared to put!(chan, 42), isn’t it?

While close() might not be changing a data structure, from a users perspective it semantically changes the channel just as much as put!() does. If it would not be mutating the channel (it would be a “pure” function), a put!() after a close() must not fail. I would argue that close!() is a better name for that function.


If we put ! in the name of every function that mutated any of its arguments (or even just the first one), there’d be so many functions with ! that it’d be useless as an indicator of mutation.


That is the convention though and we do follow it in Base at least for mutable data structures.