Shorthand for "function" keyword

Laurentiu Taschina source.spider at gmail.com
Fri Nov 10 19:25:42 UTC 2017


> You're not going to have any luck getting `func`, `fn`, or `def` as a
keyword for this. Too much existing code would break. I'm certain I've used
all of those even just in my own code. Just about *any* short keyword is
going to have the same problem.

I think understand what you're thinking. When you say existing code will
break I assume you're referring to "blah blah is a keyword" errors correct?

If so then I present to you the following question: do compilers actually
care for it to be a RESTRICTED keyword?

Is "keyword" always being synonymous with "cant be used as a
variable/property/whatever" name a restriction that actually makes sense
for all keywords in the language or is it a artifact of simpler times and
javascripts origins. Well, of early programming compilers in general really
(or assumptions when creating a language), that we never got rid of--even
if there is a good reason, I'm pretty sure everyone learned it as a
the-thing-you-do before they learned the why you do it.

But getting back to the issue. With how much tit and tat I hear every time
someone talks about the browser javascript engines in a conference
(compiles, re-compiles, optimization pipelines, this-and-that) you'd think
that compilers are sophisticated enough these days that the only reason the
compilers "care" is because they literally have an if statement somewhere
which checks if it should slap you in the face with "keywords cant be used
as variable names", and no other reason.


*Solution #1: Implement it as a unrestricted keyword*

The following example just to prove a point, but if you use func as a
function and variable in the same context in real life maybe the compiler
should actually slap you ;) since for backwards compatibility we only
really need to support it acting as a variable and for it as a feature we
only need it for acting as a synonym for function (both simultaneously is a
non-feature).


func func(func) {                                       // keyword name
argument

    func world() {                                      // keyword name

       return " world";

    }

    let fn = func func() {                              // variable keyword
name
       func = func + world();                           // argument argument
       let fn = {                                       // local-variable
            func: fn func() => func (func) {            // property keyword
name keyword argument
               return func;                             // local-argument
            }
       }
       console.log(fn.func());                          // variable
property/function-call
    }
    fn();
}

func("hello")                                           // function-call


I've included fn as a keyword for named inline functions just to make the
example even more func-y.

Also, as already explained, please note that cases "func: fn func() => func
(func) {" will never happen in a backwards compatibility case since code
like that is invalid outside of the aliases existing. They're there to answer
questions "but if people do X after they are in the language" but honestly
the compiling throwing a fit over it is perfectly reasonable there.

Naive rules: it's a synonym for "function" when it's <keyword> <name> (
<anything> ) => OR <keyword> ( <anything> ) {

I think you can apply the rule to just about any keyword you want. In fact
you can probably have it as any-random-word (as in all) and it would still
work, if you really wanted to (which nobody does of course). I'll spare
everyone the silly example.


*Solution #2: Implement it as a optional keyword*

The idea is that you keep the restricted bit of being a keyword but when
it's encountered it disables the other posibility. This works on the basis
that you never need it as both a variable (backwards compatibility) or as a
function (feature) at the same time.

Rule: func is a keyword so long as there isn't a variable declared before
it is used in that scope. If it's used as a function keyword then it can no
longer be used as a variable and vice versa. It can always be used as an
object property.


So if you have this code:

func something() {
    let func = 1; // type error: func is a keyword because it was used as a
function on line X
}

function something(func) {
    func hello() { // type error: func can not be used as a keyword because
it exists as a variable inside this scope
    }
}

function somethingElse() {
    func func() { // type error: func was used as a keyword, can not be
used as a function name in this scope
    }
}

function a() {
   function b() {
      let func = 1;
   }
   func c() { // this is ok, restriction only applies inside b
   }
}

func x() {

   function y(func) { // type error: func was already used as a function
keyword
   }

}





On Fri, Nov 10, 2017 at 6:57 PM, T.J. Crowder <
tj.crowder at farsightsoftware.com> wrote:

> On Fri, Nov 10, 2017 at 4:47 PM, Allen Wirfs-Brock
> <allen at wirfs-brock.com> wrote:
> >
> > An idiom that assigns names to arrow functions arguments:
>
> Heh, I actually *wrote* that at one point in my reply to Laurentiu, then
> removed it because it was still clunky. :-) It's less clunky when you
> combine the declarations like that...
>
> -- T.J. Crowder
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20171110/cf179e58/attachment-0001.html>


More information about the es-discuss mailing list