RCR 229: Mixf-fix syntax, if-then-else etc. as methods, fewer keywords
submitted by itsme213 on Thu Mar 11 2004 02:27:44 PM -0800
Status: rejected
Abstract
This RCR allows users to define methods with mix-fix syntax (post-fix, pre-fix, in-fix). It allows 2.0 keyword arguments. It uses this to replace some special forms (e.g. if-else) with normal method definitions.
Problem
Ruby has mostly postfix (x.y), some infix (x[5]=0), and some prefix (not x). It also has special forms for familiarity (if-then-) where Smalltalk has just methods ifTrue:[...]. In addition to these special cases, the user cannot define their own prefix or infix methods.
Proposal
Allow users to define mix-fix methods including self and keyword arguments
- unless unambiguous, all params in method def must be in parenthesis
- allow method name to be split before, between, and after parameters
- allow keyword params in any position
self
is an optional distinguished parameter indicating position of receiver in method invocation
Examples of method definitions:
def at(i)put(v)
def at(i, put:)
def post (p1:, p2:)...
def if (self:, then:)...
def if(self)then(&block)
def prefix (self:)...
def mix (self, post:)...
def mix (self) post (y)
def [ (index) ] = (value)
Allow corresponding mix-fix invocations, parenthesis required only when otherwise ambiguous. Examples below correspond to method definitions above, and use
R
as receiver
R.at(i) put (5)
R.at(i, put: 5)
R.post (p1:1, p2:2)
if R then: proc
if R then {...}
prefix R
mix R post: y
mix R post y
R [ 5 ] = 0
Analysis
- Big change, requires incremental grammar approach to parsing, will likely introduce incompatibilities
- split method names (e.g. in mix-fix mode) and keyword arguments have some overlap.
Implementation
Two implementations of incremental grammar definition and parsing, both quite doable in Ruby, but I don't know current parser constraints:
- http://research.microsoft.com/Users/ luca/Papers/FSubImp.A4.pdf
- http://www.hpl.hp.com/techreports/Compaq-DEC/SRC-RR-121.html
Clases (e.g. in modules) can introduce custom mix-fix syntax. Module 'require' chains have corresponding grammar definition incrementally built up. Ruby's current post-fix and pre-defined operators would be "built-in" by virtue of built-in library modules being required first.
The two method defs with "self:" should instead be "self".
Although I admit this is an interesting idea, applying this to Ruby makes it different language. I really want to see a language designed with this idea, to prove (or disprove) this is good. But not in Ruby.
Ruby will stay Ruby, even in Ruby2 era.
--matz.
Vote for this RCR
The two method defs with "self:" should instead be "self".
Although I admit this is an interesting idea, applying this to Ruby makes it different language. I really want to see a language designed with this idea, to prove (or disprove) this is good. But not in Ruby.
Ruby will stay Ruby, even in Ruby2 era.
--matz.
Back to RCRchive.
RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog