Make signs ">" and "<" transferrable as variables

My idea is to make signs ">" and "<" be transferrable as variables. The purpose of that is to make ">" and "<" comparisons be dependent on the variables that you have passed without extra if statements. So you can avoid stuff like that:

Example:
function bigger_or_smaller(number, biggerOrSmaller, threshold){
return number biggerOrSmaller threshold;
}
bigger_or_smaller(3, >, 6) // false (3 > 6)
bigger_or_smaller(3, <, 6) // true (3 < 6)

Also I would like to make signs revertable like booleans. For example:

!> equals <=
!< equals >=
!(>) equals <
!(<) equals >

Example:

function bigger_or_smaller(number, biggerOrSmaller, threshold){
return number biggerOrSmaller threshold;
}
let sign = >;
bigger_or_smaller(3, sign, 6); // false (3 > 6)
bigger_or_smaller(3, !sign, 6); // true (3 <= 6)
bigger_or_smaller(6, !sign, 6); // true (6 <= 6)
bigger_or_smaller(6, !(sign), 6); // false (6 < 6)

This would be a major change to the language, requiring a significant investment of time (and money).

For example what should happen in these situations?

function example(a, b, c) {
  return a b c;
}
example(1, 2, 3);
example(>, >, >);
example(1, 2, >);

or this:

function example(a) {
  console.log(typeof a); // what should this print?
}
example(<);

or this:

function example(...args) {
  console.log(args);
}
example(<);

A very strong case would need to be made to demonstrate the advantage over writing code like this:

function bigger_or_smaller(a, operator, b){
   assert(operator === '<' || operator == '>');
   return new Function('a', 'b', `return a ${operator} b`)(a, b);
}
bigger_of_smaller(1, '<', 2);

or to avoid dynamic code creation:

function bigger_or_smaller(a, operator, b){
   if (operator === '<') {
     return a < b;
   }
   if (operator === '>') {
     return a > b;
   }
   throw new Error('operator must me > or <');
}

The major problem with such a language extension is that number biggerOrSmaller threshold should not be valid syntax if we hope to introduce new operators in further language revisions.

Why don't you simply use functions? You can already pass those around as you want.

const lt = (a, b) => a < b;
const gt = (a, b) => a > b;
const not = (sign) => (a, b) => !sign(a, b);
const rev = (sign) => (a, b) => sign(b, a);

function example(a, sign, b) {
   console.log(sign(a, b));
}
let sign = gt;
example(3, sign, 6); // false (3 > 6)
example(3, not(sign), 6); // true (3 <= 6)
example(6, not(sign), 6); // true (6 <= 6)
example(6, rev(sign), 6); // false (6 < 6)
1 Like

function example(a, b, c) {
return a b c;
}
example(1, 2, 3); // Uncaught SyntaxError: Unexpected number
example(>, >, >); Uncaught SyntaxError: Unexpected sign
example(1, 2, >); Uncaught SyntaxError: Unexpected number

function example(a) {
console.log(typeof a); // "sign"
}
example(<);

So this syntax would only allow a function to accept precisely 3 arguments, with the 2nd being an operator? What if I wanted a function that takes a, b, and 3 operators to apply in succession?

For it to be a Syntax Error, the calling side (which might be an entirely different file/module) would have to have some kind of parseable way to determine that this was a special "operator function" - otherwise it'd be a runtime error.