85
crisz
5y

I just invented a new JavaScript operator. It's named "plus with wings", and it's used to sum to numbers without ambiguity or any need of type conversion, for example:

3 -+- "2" // 5

"2.1" -+- "4" // 6.1

"-1.1" -+- "" // -1.1

So, from now, you won't have to wonder anymore what type is that variable.

Tested on all browsers

Comments
  • 14
    Didn't know javascript had angels
  • 3
    Replace the dash with other unused symbol, it’ll look better
  • 3
    Never ever thought of doing it like that. Cool man!
  • 7
    @devTea that's actually not a dash, it's a concatenation of minus operator, plus and minus again
  • 2
    @M1sf3t yeah I heard vue is debating with each other now
  • 9
    Subtraction with wings (+-+) works too :D
  • 4
    @rendezvous yes, but the point is that, in the process, both sides get coerced to number
  • 2
    @irene No, for example:

    > '3'--1

    SyntaxError: Unexpected number '1'. Parse error.
  • 5
    @akamaru It actually doesn't work, in this case:

    '5'+-+1

    is equal to the string "5-1", since the "+" in first position is interpreted as concatenation.

    A good "minus with wings" could be:

    '3'-+-+-'1' // 2
  • 2
    @irene Also with two strings, it would throw the same parse error lol
  • 1
    @M1sf3t it has been going on for quite a few months actually
  • 1
    @rendezvous Thank you, I hadn't thought about the postfix decrement
  • 6
    Why not just use a language that doesn't have this fuckup in the first place
  • 3
    Maybe use a strictly typed variant of JS like typescript? :D
  • 1
    @irene A common use case could be adding stuff like positions and offsets of DOM objects which are string properties of the objects.

    Doing it cleaner requires checking whether the properties exist, parseint to get the integer part if they exist or 0 otherwise, adding up, then adding "px" to the result.
  • 3
    OMG! What kind of sorcery is this?
  • 2
    @irene sure - I don't mind such type conversions where necessary, but some people who grew up with potpourri languages don't even really know what types are.
  • 1
    @irene I'm actually a fan of the sentinel pattern to reduce runtime checks especially in hot spots.
  • 2
    @irene Say you have an unsorted integer array and look for a specific number, i.e. linear search. So you make a for loop over the array and break out if the desired integer has been found.

    OR! You could initialise the element after the last array element with the desired integer, i.e. the sentinel, and then do a while loop. This saves the boundary check for the loop variable because it will latest terminate at the sentinel.

    If you can't reserve one element more actual space behind the list, you can still check whether the last element is the desired one. If so, you're done. Otherwise, you overwrite the last element with the sentinel. Or you could save the last element in a temporary variable if the array must not be modified by the callee.

    The underlying pattern is surprisingly versatile and not only suitable for array looping.
  • 1
    @irene if the data must not be overwritten, you can save that in a temporary variable and restore it later. That's irrelevant with larger arrays, but you spare a comparison in every loop run.

    If you design the caller so that there's always room for at least one more element than the list has, then you can even cut out the temp variable.
  • 1
    So... you invented Perl?!
  • 1
    really?
  • 1
  • 0
    @Elena-K You know. Binary approximations.
  • 0
    How about /*/ or */*
Add Comment