[Discuss] Formatter always format one-line method to multi-line, is it necessary?

Following one-line method basically a ruby alias alternative, one-line is good.

def go_to
end

def goto; go_to; end
def go; go_to; end

But will always be format to following form:

def go_to
end

def goto
  go_to
end

def go
  go_to
end

I consider it’s unnecessary

Related [RFC] endless method definition · Issue #9080 · crystal-lang/crystal · GitHub.

Crystal tries to avoid aliases, so the more Crystal thing to do would just not define them at all. If you still want them, this would probably be a good use case for delegate:

delegate :goto, :go, to: go_to

the go_to is a defined method, not a object, following code not work.

def go_to
  puts "go_to"
end

delegate :goto, :go, to: go_to

go
goto
5 | delegate :goto, :go, to: go_to
     ^
Called macro defined in /home/zw963/Crystal/share/crystal/src/object.cr:1284:3

 1284 | macro delegate(*methods, to object)

Which expanded to:

 > 16 |       
 > 17 |         def go(*args, **options)
 > 18 |           go_to.go(*args, **options)
                        ^-
Error: undefined method 'go' for Nil

I updated my post for more clear intention.

Ah right, sorry delegate won’t help here since it’s basically trying to forward the #go method to the return value of go_to. So I’d just either not define the aliases or deal with the multi-line method. Don’t really have any other options I can think of.

Hi, the intention of this post is, this is a discuss, i consider the one-line method define like def meth; another_meth; end is clear enough to place on same line, but the crystal formatter always change it to three lines, i think it’s unnecessary. :joy:

This is a subjective matter. In my opinion this:

def foo; bar; end

is less readable than this:

def foo
  bar
end

If we allow the two forms then people will start making comments like “Hey, you should make this a one-liner” and so on. By not having the option to choose everything is simplified.

8 Likes

A post was split to a new topic: Compiler error assigning nil to instance var