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