Project

General

Profile

Actions

Feature #18626

closed

注釈付き代入演算子 ()= の提案

Feature #18626: 注釈付き代入演算子 ()= の提案

Added by takiuchi (Genki Takiuchi) over 3 years ago. Updated over 3 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-dev:51170]

Description

型制約を実現するために以下のような注釈付き代入演算子を提案します。

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 

いかがでしょうか。

Updated by takiuchi (Genki Takiuchi) over 3 years ago Actions #2

  • Description updated (diff)

Updated by matz (Yukihiro Matsumoto) over 3 years ago Actions #3 [ruby-dev:51174]

  • 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言語にいかなる形であれ型宣言や型注釈を導入するつもりはありません。すみません。

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

Actions

Also available in: PDF Atom