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


Groups > comp.lang.javascript > #20067

Re: !0 versus true

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <1555585.2FmMX7gGKq@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <usenet@PointedEars.de>
Organization PointedEars Software (PES)
Date Fri, 31 May 2013 18:13:24 +0200
User-Agent KNode/4.10.2
Content-Transfer-Encoding 8Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: !0 versus true
Newsgroups comp.lang.javascript
References <2788472.6QvGpzPONO@PointedEars.de> <ed180bb2-f6be-48dc-941a-adb647bd9b6c@h5g2000vbg.googlegroups.com> <5260436.L7RB4VL119@PointedEars.de> <eba22a46-75ad-4d8f-8cf3-e1dfbaa28743@c7g2000vbv.googlegroups.com> <uaf9q855bh9uri9q6o10oa2oqi7n6lp3r7@4ax.com> <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>
Supersedes <2208375.ayVdePgrsl@PointedEars.de>
MIME-Version 1.0
Lines 99
NNTP-Posting-Date 31 May 2013 18:13:27 CEST
NNTP-Posting-Host 7c97fa04.newsspool2.arcor-online.net
X-Trace DXC=YgRY7PF75ki]l@YUW5NBknA9EHlD;3Ycb4Fo<]lROoRa8kF<OcfhCOkRLRX_FVNK>nDZm8W4\YJNlb@mF9jNikdeIQbgJMhLo0gh0P`=dbPZ9d
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:20067

Show key headers only | 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