Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #20067

Re: !0 versus true

Message-ID <1555585.2FmMX7gGKq@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-05-31 18:13 +0200
Subject Re: !0 versus true
Newsgroups comp.lang.javascript
References (5 earlier) <025995e5-77c4-42e0-9c2d-60607ed970e9@2g2000yqr.googlegroups.com> <labcq81vnpm51ide8rsq6he340s8ukpi28@4ax.com> <eefe5719-a473-4326-812b-edc227f62c16@20g2000yqe.googlegroups.com> <dsqgq8pvu54hvftjupk0nubtf5ue591gm3@4ax.com> <koa02n$f8h$1@speranza.aioe.org>

Show all headers | View raw


[Supersedes; 1 + 0 + "" === "1"]

Christoph Becker wrote:

> John Harris wrote:
>> Scott Sauyet wrote:
>>> I'm sure I'm going to give myself a dope slap when you present one,
>>> but I can't come up with an example of the "0 + 1" issue.  Can you
>>> supply one?
>>   <snip>
>> 
>> You'll kick yourself :
>>   "" + 0 + 1    is  "01",
>>   "" + (0 + 1)  is  "1".
> 
> Or even without type conversion:
> 
>   2 * 0 + 1 !== 2 * (0 + 1)

The question not quoted here was

>> Do minifiers know this ?

I think – I do *hope* – all minifiers preserve precedence.  Otherwise they 
would really be useless.  Those are obviously _not_ equivalent expressions.

One might think that this is a case special to ECMAScript implementations 
because of the overloaded “+” operator.  There is a sort of implicit 
precedence here:

  "" + 0 + 1 ≡ ("" + 0) + 1 ≡ "0" + 1 ≡ "01"
  1 + 0 + "" ≡ (1 + 0) + "" ≡ "1" + 1 ≡ "1"

However, for example in Python 3:

----------------------------
#!/usr/bin/env python3

class MyString (str):
    def __add__ (self, rhs):
        # AIUI, "%s%s" % (self, rhs) is simpler but deprecated in Py3k
        return MyString("{0}{1}".format(self, rhs))

s = MyString()

# 01\n
print(s + 0 + 1 + "\n")
----------------------------

Or in C++ (compiled with g++ on GNU/Linux):

----------------------------
#include <string>
#include <iostream>
#include <sstream>
using namespace std;

string operator+ (const string& lhs, const int& rhs) {
  ostringstream converter;
  converter << lhs << rhs;
  return converter.str();
}

string operator+ (const int& lhs, const string& rhs) {
  ostringstream converter;
  converter << lhs << rhs;
  return converter.str();
}

int main () {
  string s = "";

  /* 01\n */
  cout << s + 0 + 1 << endl;

  /* 1\n */
  cout << 1 + 0 + s << endl;
}
----------------------------

All it takes is a programming language that overloads the “+” operator or 
allows overloading of the “+” operator :)

[I would be interested to know if and how the second case can be done in 
Python or any other non-ECMAScript language.

----------------------------
class MyInt (int):
    def __add__ (self, rhs):
        return "{0}{1}".format(self, rhs)

# "1"\n
print(MyInt(1 + 0) + "" + "\n")
----------------------------

is not what I am looking for.]

-- 
PointedEars

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

!0 versus true Andrew Poulos <ap_prog@hotmail.com> - 2013-05-23 23:31 +1000
  Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-23 07:58 -0700
  Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-23 17:53 +0200
    Re: !0 versus true Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-05-24 12:17 -0700
      Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-24 21:56 +0200
        Re: !0 versus true Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-05-24 13:13 -0700
          Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-24 22:23 +0200
      Re: !0 versus true Denis McMahon <denismfmcmahon@gmail.com> - 2013-05-24 22:08 +0000
        Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-25 07:00 +0200
          Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-25 20:04 -0700
            Re: !0 versus true "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-05-26 11:07 +0200
            Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-26 13:11 +0200
              Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-26 13:28 -0700
                Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-27 12:07 +0200
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-27 11:27 -0700
                Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-27 23:30 +0200
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-27 17:12 -0700
                Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-28 02:40 +0200
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-27 20:29 -0700
                Re: !0 versus true John Harris <niam@jghnorth.org.uk.invalid> - 2013-05-28 15:38 +0100
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-28 08:32 -0700
                Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-28 21:14 +0200
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-28 15:38 -0700
                Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-29 13:20 +0200
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-29 06:37 -0700
                Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-29 16:28 +0200
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-29 13:32 -0700
                Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-29 23:34 +0200
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-30 06:30 -0700
                Re: !0 versus true John Harris <niam@jghnorth.org.uk.invalid> - 2013-05-29 17:33 +0100
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-29 11:34 -0700
                Re: !0 versus true John Harris <niam@jghnorth.org.uk.invalid> - 2013-05-31 10:22 +0100
                Re: !0 versus true Christoph Becker <cmbecker69@gmx.de> - 2013-05-31 13:04 +0200
                Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-31 18:13 +0200
                Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-31 18:16 +0200
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-31 07:25 -0700
  Re: !0 versus true imma harper <immaterial_uk@yahoo.co.uk> - 2013-05-31 09:46 -0700
    Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-31 21:38 +0200
    Re: !0 versus true John Harris <niam@jghnorth.org.uk.invalid> - 2013-06-01 17:37 +0100
  Re: !0 versus true Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-06-02 21:03 +0100
    Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-06-03 04:45 -0700
      Re: !0 versus true Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-06-04 17:23 +0100

csiph-web