Re: Re: Flags enums

2020-08-26 Thread Matheus Dias de Souza
I was aware of TypeScript, but with Enum/FlagsEnum you can still do: ```o.collisionType = ‘circle’;``` ... As long as there’s this code in O: ```set collisionType(v) { this._type = CollisionType(v); }``` When some f() takes a CollisionType, it takes any argument and calls CollisionType(v). A database ORM for example could even auto generate this based on prop descriptors. Advantages: - With FlagsEnum you can even assign Array to the enum.- The enum will support different access manners (CollisionType.RIGID_BODY, 'rigidBody’). And you can continue using string literal, too, until you need to convert it to CollisionType! Enviado do Email para Windows 10 
___
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss


Re: Re: Flags enums

2020-08-26 Thread Jacob Bloom
For what it's worth, TypeScript has Enums built-in and I find myself using
string literal types instead:

```typescript
type CollisionType = 'CIRCLE' | 'CUBIC_BEZIER_CURVE' | 'RIGID_BODY';

function handleCollision(type: CollisionType) {
  if (type === 'CIRCLE') {
// ...
  } else if (type === 'SQUARE') { // Tooling errors on this line
// ...
  }
}
```

In VSCode (and I assume other IDEs that support TypeScript) you can even do
this in vanilla JS with structured JSDoc comments and the tooling will type
check for you:

```javascript
// @ts-check

/** @typedef {'CIRCLE' | 'CUBIC_BEZIER_CURVE' | 'RIGID_BODY'} CollisionType
*/

/**
 * @param {CollisionType} type
*/
function handleCollision(type) {
  if (type === 'CIRCLE') {
// ...
  } else if (type === 'SQUARE') { // Tooling errors on this line
// ...
  }
}
```

As far as I know, comparing strings is nearly as performant in most JS
engines as comparing numbers or objects because they use a method called
"string interning"

On Wed, Aug 26, 2020 at 2:25 PM Matheus Dias de Souza 
wrote:

>
>
> The com.siteblade.util package contains an approximation of this in
> ECMAScript, with no difference, except the valueOf() method (which returns
> String for working with equality), so it ends up with an additional getter
> ‘number’.
>
>
>
> https://www.npmjs.com/package/com.siteblade.util
>
>
>
> You can use either FlagsEnum or Enum.
>
>
>
> import { Enum } from 'com.siteblade.util';
>
>
>
> const CollisionType = Enum('CollisionType', [
>
> ['CIRCLE'],
>
> ['CUBIC_BEZIER_CURVE', [10]],
>
> ['RIGID_BODY', ['rigidBody', 2]]
>
> ]);
>
>
>
> var type = CollisionType('cubicBezierCurve');
>
> console.log( type == 'cubicBezierCurve' );
> ___
> es-discuss mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
>
___
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss


Re: Re: Flags enums

2020-08-26 Thread Matheus Dias de Souza
 The com.siteblade.util package contains an approximation of this in ECMAScript, with no difference, except the valueOf() method (which returns String for working with equality), so it ends up with an additional getter ‘number’. https://www.npmjs.com/package/com.siteblade.util You can use either FlagsEnum or Enum. import { Enum } from 'com.siteblade.util'; const CollisionType = Enum('CollisionType', [    ['CIRCLE'],    ['CUBIC_BEZIER_CURVE', [10]],    ['RIGID_BODY', ['rigidBody', 2]]]); var type = CollisionType('cubicBezierCurve');console.log( type == 'cubicBezierCurve' );
___
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss


Re: Flags enums

2020-08-26 Thread Hydroper
The com.siteblade.util package contains an approximation of this in
ECMAScript.

https://www.npmjs.com/package/com.siteblade.util

You can use either FlagsEnum or Enum.

import { Enum } from 'com.siteblade.util';

const CollisionType = Enum('CollisionType', [
['CIRCLE'],
['CUBIC_BEZIER_CURVE', [10]],
['RIGID_BODY', ['rigidBody', 2]]
]);

var type = CollisionType('cubicBezierCurve');
console.log( type == 'cubicBezierCurve' );

valueOf() in this case will return String. If you need number, use the
'number' property.

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


Flags enums

2020-08-20 Thread Hydroper
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;
}
```
___
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss