ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:115671] [Ruby master Feature#20054] Replace the use of `def` in endless method definitions with a new sigil
@ 2023-12-09  6:21 sawa (Tsuyoshi Sawada) via ruby-core
  2023-12-09 10:05 ` [ruby-core:115672] " vo.x (Vit Ondruch) via ruby-core
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: sawa (Tsuyoshi Sawada) via ruby-core @ 2023-12-09  6:21 UTC (permalink / raw
  To: ruby-core; +Cc: sawa (Tsuyoshi Sawada)

Issue #20054 has been reported by sawa (Tsuyoshi Sawada).

----------------------------------------
Feature #20054: Replace the use of `def` in endless method definitions with a new sigil
https://bugs.ruby-lang.org/issues/20054

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
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:

```rb
def foo = method_body
```

I propose to write

```rb
: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:

```rb
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:

```rb
def foo = bar and baz
```

is interpreted as:

```rb
(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.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:115672] [Ruby master Feature#20054] Replace the use of `def` in endless method definitions with a new sigil
  2023-12-09  6:21 [ruby-core:115671] [Ruby master Feature#20054] Replace the use of `def` in endless method definitions with a new sigil sawa (Tsuyoshi Sawada) via ruby-core
@ 2023-12-09 10:05 ` vo.x (Vit Ondruch) via ruby-core
  2023-12-09 12:13 ` [ruby-core:115674] " rubyFeedback (robert heiler) via ruby-core
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: vo.x (Vit Ondruch) via ruby-core @ 2023-12-09 10:05 UTC (permalink / raw
  To: ruby-core; +Cc: vo.x (Vit Ondruch)

Issue #20054 has been updated by vo.x (Vit Ondruch).


How would you like to find such method in a code, e.g. using `grep`?

I might be getting old, but I still prefer:

~~~
{:foo => bar}

foo.each {|bar| ... }

def foo
  ...
end
~~~

Instead all the new shorter syntaxes.

----------------------------------------
Feature #20054: Replace the use of `def` in endless method definitions with a new sigil
https://bugs.ruby-lang.org/issues/20054#change-105606

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
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:

```rb
def foo = method_body
```

I propose to write

```rb
: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:

```rb
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:

```rb
def foo = bar and baz
```

is interpreted as:

```rb
(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.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:115674] [Ruby master Feature#20054] Replace the use of `def` in endless method definitions with a new sigil
  2023-12-09  6:21 [ruby-core:115671] [Ruby master Feature#20054] Replace the use of `def` in endless method definitions with a new sigil sawa (Tsuyoshi Sawada) via ruby-core
  2023-12-09 10:05 ` [ruby-core:115672] " vo.x (Vit Ondruch) via ruby-core
@ 2023-12-09 12:13 ` rubyFeedback (robert heiler) via ruby-core
  2023-12-09 12:15 ` [ruby-core:115675] " rubyFeedback (robert heiler) via ruby-core
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: rubyFeedback (robert heiler) via ruby-core @ 2023-12-09 12:13 UTC (permalink / raw
  To: ruby-core; +Cc: rubyFeedback (robert heiler)

Issue #20054 has been updated by rubyFeedback (robert heiler).


> Thence, we should get rid of def

I am not sure this is possible. People kind of adjusted to
def.

> Within Ruby syntax, symbol is naturally used to represent
> a method name

While I agree, symbols are also used elsewhere, such as for
arguments to methods.

For instance, in a few projects I use an API such as this:

    enable :colours

(Which in turn calls the method enable_colours; I have a generic
enable method, and then use Symbols for that to designate entry
points. Correspondingly I also then use: disable :colours to
toggle onto the alternate state.)

> making the endless method definition syntax look superficially
> like an "assignment to a symbol" would make sense.

Personally I don't like endless method definitions, but that is 
ultimately subject of opinions. mame probably likes endless methods.

I think one issue I see with the proposal here is, if we look at this
suggestion:

    :foo = method_body

Then "def" is gone; there is no "define_method" either. I am not sure
this is good. We should keep in mind how people who use ruby - or are
new to ruby - look at syntax. "def" standing for "define a method" is
quite simple to understand, in my opinion. Python also uses "def". I
quite like "def" (I use define_method only programmatically when I 
have to define dynamic methods).

Perhaps I am becoming too conservative with old age, but I prefer more
the oldschool ruby variant here - keeping it simple. Just "def".

In regards to omitting "end": I can kind of understand people wanting
to drop "end" just as I can understand people who prefer:

    def foo :bar

rather than:

    def foo(:bar)

But the saving a few characters should not really be the most compelling
argument. I assume - and perhaps mame can comment - that one advantage
of endless method definition is that it may be easier to write and help
with code golfing somewhat.

----------------------------------------
Feature #20054: Replace the use of `def` in endless method definitions with a new sigil
https://bugs.ruby-lang.org/issues/20054#change-105608

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
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:

```rb
def foo = method_body
```

I propose to write

```rb
: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:

```rb
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:

```rb
def foo = bar and baz
```

is interpreted as:

```rb
(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.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:115675] [Ruby master Feature#20054] Replace the use of `def` in endless method definitions with a new sigil
  2023-12-09  6:21 [ruby-core:115671] [Ruby master Feature#20054] Replace the use of `def` in endless method definitions with a new sigil sawa (Tsuyoshi Sawada) via ruby-core
  2023-12-09 10:05 ` [ruby-core:115672] " vo.x (Vit Ondruch) via ruby-core
  2023-12-09 12:13 ` [ruby-core:115674] " rubyFeedback (robert heiler) via ruby-core
@ 2023-12-09 12:15 ` rubyFeedback (robert heiler) via ruby-core
  2023-12-10 14:59 ` [ruby-core:115681] " nobu (Nobuyoshi Nakada) via ruby-core
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: rubyFeedback (robert heiler) via ruby-core @ 2023-12-09 12:15 UTC (permalink / raw
  To: ruby-core; +Cc: rubyFeedback (robert heiler)

Issue #20054 has been updated by rubyFeedback (robert heiler).


I should also say that I understand sawa's argument too; the

    def foo = method_body

really does look a bit weird without "end". My eyes (or rather my
brain) have adapted to assuming a nice "end" being part of a method.
It does not affect me personally, though, as I don't use that
syntax variant. I stick with oldschool "def foo; end".

----------------------------------------
Feature #20054: Replace the use of `def` in endless method definitions with a new sigil
https://bugs.ruby-lang.org/issues/20054#change-105609

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
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:

```rb
def foo = method_body
```

I propose to write

```rb
: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:

```rb
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:

```rb
def foo = bar and baz
```

is interpreted as:

```rb
(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.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:115681] [Ruby master Feature#20054] Replace the use of `def` in endless method definitions with a new sigil
  2023-12-09  6:21 [ruby-core:115671] [Ruby master Feature#20054] Replace the use of `def` in endless method definitions with a new sigil sawa (Tsuyoshi Sawada) via ruby-core
                   ` (2 preceding siblings ...)
  2023-12-09 12:15 ` [ruby-core:115675] " rubyFeedback (robert heiler) via ruby-core
@ 2023-12-10 14:59 ` nobu (Nobuyoshi Nakada) via ruby-core
  2023-12-20  8:05 ` [ruby-core:115807] " matz (Yukihiro Matsumoto) via ruby-core
  2024-01-04  5:58 ` [ruby-core:116006] " duerst via ruby-core
  5 siblings, 0 replies; 7+ messages in thread
From: nobu (Nobuyoshi Nakada) via ruby-core @ 2023-12-10 14:59 UTC (permalink / raw
  To: ruby-core; +Cc: nobu (Nobuyoshi Nakada)

Issue #20054 has been updated by nobu (Nobuyoshi Nakada).


sawa (Tsuyoshi Sawada) wrote:
> Within Ruby syntax, symbol is naturally used to represent a method name.

Yes, because a symbol is a **name**.
Not a method.

----------------------------------------
Feature #20054: Replace the use of `def` in endless method definitions with a new sigil
https://bugs.ruby-lang.org/issues/20054#change-105614

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
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:

```rb
def foo = method_body
```

I propose to write

```rb
: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:

```rb
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:

```rb
def foo = bar and baz
```

is interpreted as:

```rb
(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.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:115807] [Ruby master Feature#20054] Replace the use of `def` in endless method definitions with a new sigil
  2023-12-09  6:21 [ruby-core:115671] [Ruby master Feature#20054] Replace the use of `def` in endless method definitions with a new sigil sawa (Tsuyoshi Sawada) via ruby-core
                   ` (3 preceding siblings ...)
  2023-12-10 14:59 ` [ruby-core:115681] " nobu (Nobuyoshi Nakada) via ruby-core
@ 2023-12-20  8:05 ` matz (Yukihiro Matsumoto) via ruby-core
  2024-01-04  5:58 ` [ruby-core:116006] " duerst via ruby-core
  5 siblings, 0 replies; 7+ messages in thread
From: matz (Yukihiro Matsumoto) via ruby-core @ 2023-12-20  8:05 UTC (permalink / raw
  To: ruby-core; +Cc: matz (Yukihiro Matsumoto)

Issue #20054 has been updated by matz (Yukihiro Matsumoto).

Status changed from Open to Rejected

I reject this proposal, because method definitions are tightly coupled with `def` keyword under the current syntax. Besides that, `:` sigil is for symbols which not always coupled with methods, but identifiers in general. As a result, the proposed new syntax does not help readability nor understandability (from my POV).

Matz.


----------------------------------------
Feature #20054: Replace the use of `def` in endless method definitions with a new sigil
https://bugs.ruby-lang.org/issues/20054#change-105756

* Author: sawa (Tsuyoshi Sawada)
* Status: Rejected
* Priority: Normal
----------------------------------------
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:

```rb
def foo = method_body
```

I propose to write

```rb
: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:

```rb
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:

```rb
def foo = bar and baz
```

is interpreted as:

```rb
(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.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:116006] [Ruby master Feature#20054] Replace the use of `def` in endless method definitions with a new sigil
  2023-12-09  6:21 [ruby-core:115671] [Ruby master Feature#20054] Replace the use of `def` in endless method definitions with a new sigil sawa (Tsuyoshi Sawada) via ruby-core
                   ` (4 preceding siblings ...)
  2023-12-20  8:05 ` [ruby-core:115807] " matz (Yukihiro Matsumoto) via ruby-core
@ 2024-01-04  5:58 ` duerst via ruby-core
  5 siblings, 0 replies; 7+ messages in thread
From: duerst via ruby-core @ 2024-01-04  5:58 UTC (permalink / raw
  To: ruby-core; +Cc: duerst

Issue #20054 has been updated by duerst (Martin Dürst).





What about using '&', i.e. resulting in `&foo = method_body`? '&' is definitely associated with methods/blocks/procs, both in the parameter list of a method definition and in the `&:method_name` idiom. 



----------------------------------------

Feature #20054: Replace the use of `def` in endless method definitions with a new sigil

https://bugs.ruby-lang.org/issues/20054#change-106000



* Author: sawa (Tsuyoshi Sawada)

* Status: Rejected

* Priority: Normal

----------------------------------------

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:



```rb

def foo = method_body

```



I propose to write



```rb

: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:



```rb

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:



```rb

def foo = bar and baz

```



is interpreted as:



```rb

(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.







-- 

https://bugs.ruby-lang.org/

 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-01-04  5:58 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-09  6:21 [ruby-core:115671] [Ruby master Feature#20054] Replace the use of `def` in endless method definitions with a new sigil sawa (Tsuyoshi Sawada) via ruby-core
2023-12-09 10:05 ` [ruby-core:115672] " vo.x (Vit Ondruch) via ruby-core
2023-12-09 12:13 ` [ruby-core:115674] " rubyFeedback (robert heiler) via ruby-core
2023-12-09 12:15 ` [ruby-core:115675] " rubyFeedback (robert heiler) via ruby-core
2023-12-10 14:59 ` [ruby-core:115681] " nobu (Nobuyoshi Nakada) via ruby-core
2023-12-20  8:05 ` [ruby-core:115807] " matz (Yukihiro Matsumoto) via ruby-core
2024-01-04  5:58 ` [ruby-core:116006] " duerst via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).