[ruby-dev:51174] [Ruby master Feature#18626] 注釈付き代入演算子 ()= の提案

From: "matz (Yukihiro Matsumoto)" <noreply@...>
Date: 2022-03-20 14:24:50 UTC
List: ruby-dev #51174
Issue #18626 has been updated by matz (Yukihiro Matsumoto).

Status changed from Open to Rejected

Very interesting proposal both syntax-wise and semantic-wise. But I have decided that we are not going to introduce any kind of syntax annotation in the language, sorry.

Matz.

文法的にも意味論的にも大変興味深い提案ですが、Ruby言語にいかなる形であれ型宣言や型注釈を導入するつもりはありません。すみません。

まつもと ゆきひろ /:|)


----------------------------------------
Feature #18626: 注釈付き代入演算子 ()= の提案
https://bugs.ruby-lang.org/issues/18626#change-96946

* Author: takiuchi (Genki Takiuchi)
* Status: Rejected
* Priority: Normal
----------------------------------------
型制約を実現するために以下のような注釈付き代入演算子を提案します。

```
class Object
  def self.()= (what)
    what.is_a? self or raise TypeRestrictionError
  end
end

age (Fixnum) = 30

def add(a(Numeric), b(Numeric))
  a + b
end
add 1, "2" # raises TypeRestrictionError
```

左辺値の後に`(...)` が続いた場合に`()`内の値に対して`()=`演算子を呼び出します。
Rubyはメソッドの返り値が左辺値になることは無いので既存の文法とは衝突しないかなと。

以下のようなenum的な制約も便利だと思います。

```
class Array
  def ()= (what)
    self.include? what or raise ValueRestrictionError
  end
end

flag([1,2,3]) = 3
```

いかがでしょうか。



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

In This Thread

Prev Next