# Add Channel#\<\< as an alias to Channel#send

**URL:** <https://forum.crystal-lang.org/t/add-channel-as-an-alias-to-channel-send/1072>\
**Category:** Crystal Contrib\
**Created:** [August 27, 2019, 2:25pm UTC](https://forum.crystal-lang.org/t/add-channel-as-an-alias-to-channel-send/1072 "2019-08-27T14:25:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![j8r](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/j8r/32/111_2.png) [@j8r](https://forum.crystal-lang.org/u/j8r)\
**Post date:** [August 27, 2019, 2:25pm UTC](https://forum.crystal-lang.org/t/add-channel-as-an-alias-to-channel-send/1072/1 "2019-08-27T14:25:39Z")

</div>

The suggestion here is to have something like Go `channel <- value`.

Usually I’m against aliases, but `<<` is already quite widespread an used in the stdlib to append/push a value (`Array#<<`, `IO#<<`). Furthermore, by returning `self`, we can even have the possibility to send several values in one line, like with `IO#<<`.

```auto
class Channel
  def <<(value : T) : Channel
    send value
    self
  end
end

channel = Channel(Int32).new
spawn do
  channel << 0
  # Same as
  channel.send 0

  # Multiple sends
  channel << 0 << 0
end
4.times do
  puts channel.receive # => 0
end

```

---

<div class="post-metadata">

**Author:** ![stronny](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/stronny/32/228_2.png) [@stronny](https://forum.crystal-lang.org/u/stronny)\
**Post date:** [August 27, 2019, 5:25pm UTC](https://forum.crystal-lang.org/t/add-channel-as-an-alias-to-channel-send/1072/2 "2019-08-27T17:25:22Z")

</div>

The problem with operator overloading from where I stand is that whenever the new semantics are substantially different from the original the result is non-intuitive.

Consider this:

```auto
class Channel
  def <<(value : T) : Channel
    send value
    self
  end
end

x = 1
p x << 4 # => 16
p x << 2 << 4 # => 64
x = Channel::Buffered(Int32).new(10)
p x << 1 << 2 << 4 # => #<Channel::Buffered(Int32):0xcb6ea0>

```

Having said that I don’t think this particular operator will be a notable issue exactly because as you’ve pointed out it’s already widely understood to do a “push” operation as well as the bit shift.

But just for being a contrarian my counter suggestion would be to remove the `<<` overloads from Array, IO, Set, Deque and other non-numbers. 😛

---

<div class="post-metadata">

**Author:** ![alex-lairan](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/alex-lairan/32/218_2.png) [@alex-lairan](https://forum.crystal-lang.org/u/alex-lairan)\
**Post date:** [August 28, 2019, 9:48am UTC](https://forum.crystal-lang.org/t/add-channel-as-an-alias-to-channel-send/1072/3 "2019-08-28T09:48:13Z")

</div>

In C++, the `<<` operator is used for streams.

`std::cout << "hello" << std::endl;`

I think it’s natural when you knew it, but hard to understand when not.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [September 4, 2019, 8:03pm UTC](https://forum.crystal-lang.org/t/add-channel-as-an-alias-to-channel-send/1072/4 "2019-09-04T20:03:44Z")

</div>

Do you know any practical example where chaining channel sends would be useful?

---

<div class="post-metadata">

**Author:** ![absolutejam\_gh](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/absolutejam_gh/32/501_2.png) [@absolutejam\_gh](https://forum.crystal-lang.org/u/absolutejam_gh)\
**Post date:** [September 7, 2019, 1:47pm UTC](https://forum.crystal-lang.org/t/add-channel-as-an-alias-to-channel-send/1072/5 "2019-09-07T13:47:03Z")

</div>

I really like this. It seems idiomatic and doesn’t impose a large learning curve or understanding overhead. I actually think Go’s version is a little less obvious when skimming as they have very similar syntax to send and recieve.

I didn’t know about the \<\< syntax when I started (Not a Rubyist) but it’s pretty self explanatory and obvious.
