# Endless methods for Crystal

**URL:** <https://forum.crystal-lang.org/t/endless-methods-for-crystal/3087>\
**Category:** Community\
**Created:** [March 25, 2021, 5:07pm UTC](https://forum.crystal-lang.org/t/endless-methods-for-crystal/3087 "2021-03-25T17:07:17Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![mavu](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/mavu/32/213_2.png) [@mavu](https://forum.crystal-lang.org/u/mavu)\
**Post date:** [December 9, 2023, 1:41pm UTC](https://forum.crystal-lang.org/t/endless-methods-for-crystal/3087/21 "2023-12-09T13:41:26Z")

</div>

What is wrong with

```auto
def some_method(ladidah); return ladidah; end

```

> However, the views of the Ruby community developed in the way that using `;` is deemed bad taste: it is a sign that you are cramming too much—several logical **phrases** —into one line

And I don’t think this argument makes any sense, because the new syntax in 3.0 does _exactly_ the same. it just looks different, and in my opinion the above is easier and quicker to understand.

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [December 9, 2023, 6:45pm UTC](https://forum.crystal-lang.org/t/endless-methods-for-crystal/3087/22 "2023-12-09T18:45:45Z")

</div>

Matz has been very accommodating in allowing different styles of  
programming to be available for Ruby users because he designed it  
to create programmer’s happiness.

People can read the story of the inclusion of `endless methods` and  
the ongoing discussion of its use and/or necessity. But it always comes  
down to allowing programmer choice, because it’s certainly `not necessary`.

However, let me provide a specific case where I think it would make the  
Crystal source code more concise, shorter, and argurably more readable.

In the Crystal sourece file:

> <https://github.com/crystal-lang/crystal/blob/master/src/int.cr>

there is this code.

```auto
  def abs : self
    self >= 0 ? self : -self
  end

  def round(mode : RoundingMode) : self
    self
  end

  def ceil : self
    self
  end

  def floor : self
    self
  end

  def trunc : self
    self
  end

  # Returns `self`.
  def round_even : self
    self
  end

  # Returns `self`.
  def round_away
    self
  end

  # :inherit:
  #
  # Always returns `true` for `Int`.
  def integer? : Bool
    true
  end  

```

As in some of the Ruby blog examples using `endless methods`, this  
could easily be shortened, with no loss of understanding, as below.

```auto
  def (abs : self) = self >= 0 ? self : -self

  def (round(mode : RoundingMode) : self) = self

  def (ceil : self) = self
 
  def (floor : self) = self
  
  def (trunc : self) = self
  
  # Returns `self`.
  def (round_even : self) = self
  
  # Returns `self`.
  def (round_away) = self
 
  # :inherit:
  #
  # Always returns `true` for `Int`.
  def (integer? : Bool) = true

```

Personally, I see it as a serious waste of space to use 3 loc (lines of code)  
for each method to express these simple definitions. I find the later much  
more visually appealing, but also cognitively more direct, with less coding noise.

But again, these are my personal preferences.  
However, I think it’s an attractive feature of a language|community to be amenable to  
diverse styles of programming, if it doesn’t come at great cost to implement and maintain.

---

<div class="post-metadata">

**Author:** ![HertzDevil](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hertzdevil/32/1023_2.png) [@HertzDevil](https://forum.crystal-lang.org/u/HertzDevil)\
**Post date:** [December 9, 2023, 8:21pm UTC](https://forum.crystal-lang.org/t/endless-methods-for-crystal/3087/23 "2023-12-09T20:21:55Z")

</div>

You can indeed emulate parts of this syntax using macros and the `[]=` operator: [Carcin](https://play.crystal-lang.org/#/r/g6zi)

It is so functionally similar to a read-only property (i.e. `getter`) that I don’t think there ever needs to be compiler support for it. This is syntactically impossible with a Ruby `attr_reader`, so they have to invent a new syntax for this. Crystal is already more powerful in this regard.

> [@jzakiya](#):
>
> if it doesn’t come at great cost to implement and maintain

That’s a big if, and it doesn’t stop at that; it also includes third-party code editors, syntax highlighters etc. that are not necessarily written in Crystal itself. Already the Crystal playground doesn’t handle automatic indentation correctly when there is a trailing `if`. All these little things add up to affect people’s impression of the language, its tooling, and ecosystem, especially new users, and usually in the negative direction. So “programmer happiness” is not a simple measurement based solely on the developers who are already using that language for quite some time.

---

<div class="post-metadata">

**Author:** ![kojix2](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kojix2/32/1583_2.png) [@kojix2](https://forum.crystal-lang.org/u/kojix2)\
**Post date:** [December 10, 2023, 7:52am UTC](https://forum.crystal-lang.org/t/endless-methods-for-crystal/3087/24 "2023-12-10T07:52:06Z")

</div>

I’m not very interested in Endless methods. If the Crystal language is mature enough and there is a need to improve the details of the grammar, it’s okay to have it, but I don’t mind if it’s not there until then.

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [December 10, 2023, 9:36pm UTC](https://forum.crystal-lang.org/t/endless-methods-for-crystal/3087/25 "2023-12-10T21:36:48Z")

</div>

> [@HertzDevil](#):
>
> You can indeed emulate parts of this syntax using macros and the `[]=` operator: [Carcin](https://play.crystal-lang.org/#/r/g6zi)

That’s very nice; easier than I thought it would take (though I don’t understand most of it yet).

[Previous page](https://forum.crystal-lang.org/t/endless-methods-for-crystal/3087.md?page=1)
