Set and Range objects and literals

Expanding my comment from the chaining comparitor proposal...

Currently we write:

if (longVariableName == 'p' || longVariableName == 'div') {...}
if (effectiveMousePos.x <= right && effectiveMousePos.x >= left) {...}

and it's annoyingly verbose. Instead, I'd like to write:

if (longVariableName in {{p, div}}) {...}
if (effectiveMousePos.x in [<left, right>]) {...}

Sets are kind of like maps, so you can write (I have written)

if (longVariableName in {p:1, div:1}) {...}

but the irrelevant values are distracting, so it's awkward. Doubly so if there's a formatter or style rule that insists on thinking of the set as a map.

Literals and in tests are the case I most care about, but as long as we're doing this...

  • set[key] = True/False adds or removes a key from a set
  • sets take *, +, and - operators for intersection, union, and set-difference, with obvious *=, +=, and -=.
  • for (i in set) does this obvious
  • ranges have members start, end, min, and max. The first two are ordered based on definition, the latter based on comparison.
  • for (i of range) starts at min, then adds 1 until i exceeds max
  • for (i of range.by(step)) does the same except that it adds step each time. If step is negative, it starts from max.

It's probably not worth adding syntax for this.

For your first use case, ['p', 'div'].includes(longVariableName) already exists, is sufficient, and is plenty fast (you need a surprisingly amount of data before membership tests start being slower for arrays than for hashmaps, certainly more than it would be sensible to write inline).

1 Like

How would this syntax handle logic?

if( x > left && x < right ) ...
//vs.:
if( x > left || x < right ) ...

Try this:

//Yours
if (effectiveMousePos.x <= right && effectiveMousePos.x >= left) {...}

//Alternative (loose)
if ({with(effectiveMousePos) {x <= right && x>= left}}) {...}

//Alternative (strict)
if ((({x}) => (x <= right && x>= left))(effectiveMousePos)) {...}