On Nov 10, 2013, at 3:31 PM, Benjamin (Inglor) Gruenbaum wrote:

> From: Allen Wirfs-Brock <[email protected]>
> >One of the the few remaining uses of a function's 'arguments' binding is to 
> >determine the actual number of passed arguments. This is necessary in some 
> >overloading scenarios where a function has different behavior when an 
> >argument is completely absent then it has when undefined (or any other 
> >default value) is explicitly passed in that parameter position.  
> 
> I'd just like to say that I wouldn't rule out a creative solution that'd 
> allow better function overloading. The problem is not the fact the arguments 
> number isn't reachable without the context decided arguments imho. 
> 
> The problem is that overloading is something that is very useful to do, and 
> we'd like to be able different functions based on the number of parameters. 
> While optional parameters, and rest parameters are really useful and solve a 
> lot of the use cases we'd need overloading for - it's clear that there are 
> use cases it does not address.
> 
> I'm not sure that tackling the problem by adding new syntax for the number of 
> params is the best approach (although I'd hate to have to resort to 
> arguments, explicitly or implicitly like in the example given).
> 

Declarative overloading in a dynamically typed language is tricky and 
programmers with static language experience too often try to pretend that they 
can do JS overloading just like they would do it in C++ or Java.

I think the only sort of declarative overloading that is appropriate for JS is 
based upon the number of actually passed arguments (and nothing else).  If you 
agree with that, then there is a pretty straightforward pattern for expressing 
that in  ES6:

function f(...args) {  //an overloaded frunction
  switch (args.length) {
     case 0: {
          /* body of the no arguments overload */
      }
      case 1: { let  <single parameter pattern> ]   = args;        
         /* body of the 1 argument overload */
       }
       case 2: { let [ <two parameter pattern> ] = args;
          /* body of the 2 argument overload */
        }
        case 3: {  let [ <three parameter pattern> ] = args;         
           /* body of the 3 argument overload */
         }
        ...
        default: { let [ <n + rest parameter pattern> ] = args;          
           /* body of the n+rest argument overload */
         }
   }
}

This is a pattern that would be pretty easy for engines to recognize, avoid 
creating an actual 'args' object, and map the selected cases destructed 
parameters directly onto the passed argument on the stack.

Allen




_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to