ruby picture

RCR 229: Mixf-fix syntax, if-then-else etc. as methods, fewer keyword

Submitted by itsme213 (Thu Mar 11 19:03:44 UTC 2004)

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
   <li>unless unambiguous, all params in method def must be in parenthesis </li>
        <li>allow method name to be split before, between, and after parameters </li>
        <li>allow keyword params in any position </li>
        <li><code>self</code> is an optional distinguished parameter indicating position of receiver in method invocation</li>

Examples of method definitions:

   <li>def at(i)put(v)</li>
        <li>def at(i, put:)</li>
        <li>def post (p1:, p2:)...</li>
        <li>def if (self:, then:)...</li>
        <li>def if(self)then(&block)
        </li>
        <li>def prefix (self:)...</li>
        <li>def mix (self, post:)...</li>
        <li>def mix (self) post (y)</li>
        <li>def [ (index) ] = (value)</li>

Allow corresponding mix-fix invocations, parenthesis required only when otherwise ambiguous. Examples below correspond to method definitions above, and use R as receiver

 <li>R.at(i) put (5)</li>
 <li>R.at(i, put: 5)</li>
 <li>R.post (p1:1, p2:2)</li>
 <li>if R then: proc</li>
 <li>if R then {...}</li>
 <li>prefix R</li>
 <li>mix R post: y</li>
 <li>mix R post y</li>
 <li> R [ 5 ] = 0</li>

Analysis

Implementation

Two implementations of incremental grammar definition and parsing, both quite doable in Ruby, but I don't know current parser constraints:

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.

ruby picture
Comments Current voting

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.


Strongly opposed 5
Opposed 5
Neutral 0
In favor 0
Strongly advocate 0
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .