Project

General

Profile

Actions

Feature #20054

closed

Replace the use of `def` in endless method definitions with a new sigil

Added by sawa (Tsuyoshi Sawada) about 1 year ago. Updated 12 months ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:115671]

Description

I propose to remove the use of keyword def from the syntax of endless method definition, and introduce a new sigil instead of it. There are several possibilities for what character to use as the sigil, but the most seemingly promising one to me at this point is the colon. So, instead of:

def foo = method_body

I propose to write

:foo = method_body

There a few reasons to dispense with def in endless method definition.

First, the current syntax for endless method definition looks too similar to conventional method definition. Without endless method definition, we could already define a method in a single line:

def foo; method_body end

and compared to this, what the endless method definition does is that, it only saves you from typing the end keyword just by replacing the semicolon with an equal sign. This actually had not made much sense to me. Just saving you from typing the keyword end looks too small of a change for introducing new syntax. In order for endless method definition syntax to be justified (as a shorthand for conventional method definition), it needs to save more typing.

Second, in #19392, some people are claiming to change the precedence involving endless method definition. I agree with Matz and other developers who support the current precedence in which:

def foo = bar and baz

is interpreted as:

(def foo = bar) and baz

and I understand that the controversy is due to the look and feel of the keyword def. def has lower precedence than and in conventional method definition, although = has higher precedence than and in variable/constant assignment. Mixing the low-precedence def and the high-precedence = into a single syntax was the cause of the trouble, according to my opinion.

Thence, we should get rid of def. Once we do so, we need to distinguish endless method definition from variable/constant assignment in a new way. What came to my mind was to use a single character: a sigil.

Especially, using the colon seems to make sense to me for several reasons:

Most importantly, assignment to a symbol is impossible, and it currently raises a syntax error, so it would not conflict with variable/constant assignment syntax.

Within Ruby syntax, symbol is naturally used to represent a method name. For example, in foo(&:bar) constructions, users are used to passing a method name as a symbol. Also, a method definition returns a symbol representing the method name. So, making the endless method definition syntax look superficially like an "assignment to a symbol" would make sense.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like1