Is there anything like pipe operator in Crystal?

Is there anyway of writting this:

In a better way…

At least to avoid using ‘new’, that would make it less verbose


Related: [RFC] Pipe Operator · Issue #1388 · crystal-lang/crystal · GitHub

But no, .new (or some other constructor method) is required. Hard to provide any suggestions based on this one screenshot, but is there a reason you need 3 separate Link types when only one of them is provided any data?

1 Like

When .new looks like unnecessary noise, I tend to implement the [] method on the class:


What are you trying to implement here?


Basically they all implement the abstract class Link as a interface. I’m experimenting how would it work.

Right, but would a single URLLink obj not be enough or?

I’m trying to distribute logic between classes implementing same interface, instead of using methods within the abstract class.

For example, instead of calling a method “valid?” , on the class Link, I would know that if the link is of type ValidLink, it’s validated, also MemLink is a link that’s not persisted yet. and does not know how to be persisted…May be I could have: RestLink, links that are brought from a restfull API I need to call, or RedisLink, a link that I know it’s persisted into Redis, instead of having a RedisLinkRepository…and so on.

At the end of the day, the only thing I have are implementations of abstract class Link…

I’m experimenting with this, makes some ideas clear, some verbose…

1 Like

Just for discuss pipe, Elixir is awesome do job like this:
 |> ValidLink()
 |> links.create()
1 Like

Is it possible to do it with macros?

In cases where there is no need to access MemLink or UrlLink as local variables later on, I prefer to extend the ValidLink class and add convenient class methods.

class ValidLink
  def self.create(foo, bar ...)
    ml =, bar ...)
    ul =

Extending classes from external libraries can lead to confusion, so adding comments to help understand what changes were made. Ideally, you will create a pull request so that your changes can be merged to the upstream repository.


Thanks, I guess the correct name for what I’m trying to do is , Decorator pattern, this classes, ‘decorate’ the functionality

Found this shard that’s adding some helpers: GitHub - stephendolan/decorator: A simple Crystal shard for decorating objects

1 Like

Nice shards.

Is it possible to do it with macros?

I guess it is no, because |> is not a valid identifier for Crystal, we have to change compiler to achieve this.

I remember that pipeline operators were available in Ruby at some point.

I checked to see what the current state of the attempt to introduce pipeline operators to Ruby was, and found that it had been removed.

But right assignment can still be used today.

100 => a
#=> nil
# 100
1 Like

The usage of pipe operator in Ruby (as expected by @ matz) is completely meaningless.