Flags enums

Hydroper hydroperhh at gmail.com
Fri Aug 21 00:52:06 UTC 2020


Flags enums would be primitive classes with constants (_name_:String,
_value_:Number). It'd be very, very efficient.

Of course it's easy to use POJOs (`{}`) instead of numeric values, but this
feature would only provide performance advantages.

The following program implies:

- `String(E.FNX) == 'fnx'`
- `E.FNX.valueOf() == 1`
- `String(E.QWL_QWERTY) == 'xxxQwerty'`
- `E.QWL_QWERTY.valueOf() == 32`
- `String(E.WOW_K) == 'wowK'`
- `E.WOW_K.valueOf() == 64`

```javascript
enum E {
    FNX,
    QWL_QWERTY = ['xxxQwerty', 32];
    WOW_K;

    f() {
        // Some method
    }
}

var e = E('xxxQwerty');

// constant in e
// E.prototype.set()
// E.prototype.exclude()
// E.prototype.toggle()
// E.prototype.filter()

console.log( 'xxxQwerty' in e );
console.log( ['fnx', 'xxxQwerty'] in e );
console.log( e.set('fnx').exclude('fnx').filter(['fnx', 'xxxQwerty']) );
console.log( e.toggle('xxxQwerty') );
console.log( e.valueOf() );

console.log( E(['fnx', 'xxxQwerty']) );
```

In typed languages, Array and String would implicitly convert to E.

One could define constant either by omitting expression, by an array
literal (`[name, value]` or `[value, name]`), by a string literal or by a
numeric literal. More examples:

```
enum E {
    AA_AA;
    LNK = 'vVvVvV';
    QO = 32;
}
```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20200820/84a6899f/attachment.html>


More information about the es-discuss mailing list