Skip to content

Endless method definition#17073

Draft
mamantoha wants to merge 5 commits into
crystal-lang:masterfrom
mamantoha:endless-method-def
Draft

Endless method definition#17073
mamantoha wants to merge 5 commits into
crystal-lang:masterfrom
mamantoha:endless-method-def

Conversation

@mamantoha

@mamantoha mamantoha commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Adds support for endless method definitions:

def square(x) = x * x
def square(x : Int32) : Int32 = x * x
def default_value = nil
def self.foo = 1

This is related to the existing RFC/discussion in #9080

This PR is mostly a just-for-fun experiment. The discussion in #9080 was skeptical, and I am not sure anyone actually wants this feature in Crystal. I wanted to explore what the implementation would look like.

Endless method bodies must start on the same line as =.

Invalid examples:

def foo =
  1

def foo(x) =
  x

def foo =
  1
  2
  3

This PR is not really about saving characters.

I also agree with everyone who doesn't like it: I don’t really need this change either 🙂

@mamantoha mamantoha marked this pull request as draft June 15, 2026 14:30
@yxhuvud

yxhuvud commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

I for one really want it. I see no examples with a type declaration at the end (or wherever they should be)

@mamantoha

Copy link
Copy Markdown
Contributor Author

@yxhuvud

Parser spec:

it_parses "def foo(x) : Int32 = x", Def.new("foo", args: ["x".arg], body: "x".var, return_type: "Int32".path)

Formatter spec:

assert_format "def   foo (  x  :  Int32 ) : Int32 = x", "def foo(x : Int32) : Int32 = x"

@kostya

kostya commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

def square(x); x * x; end - just 4 symbols longer.

@jneen

jneen commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

I would use this feature in Crystal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants