4 ms·
Also things like +!![] is because while JS is dynamic it does have a very strict type system! ![] becomes a boolean +true becomes a number.
by z3t4 5y ago
Also things like +!![] is because while JS is dynamic it does have a very strict type system! ![] becomes a boolean +true becomes a number.
- viraptor 5y agoThis is a result of the implicit type conversion feature rather than a strict type system.
- simiones 5y agoJS has a very weak type system, full of implicit conversions. In dynamic languages with strong type systems, like Common Lisp, such operations typically result in errors: (+ (not (not '()))) The value NIL is not of the expected type NUMBER. Similarly in Ruby: +!![] undefined method `+@' for true:TrueClass (NoMethodError) [Condition of type TYPE-ERROR] Interestingly, Python does the same thing as JS in this case, even though it is typically quite strongly typed. Edit: not quite the same, as the empty array is converted to False in Python, just as it is in Ruby (in CL, nil/'() IS the canonical false value); but still, Python outputs 0, it doesn't complain like the other two.
- epidemian 5y ago> Interestingly, Python does the same thing as JS in this case, even though it is typically quite strongly typed. [...] > Python outputs 0, it doesn't complain like the other two. yep, this is one of those Python weird bits. in Python, booleans are ints, True is 1 and 0 is False. and i don't mean it in a JS-ish way like "they can be converted to...". no, True is the integer value 1. in fact, the type bool is a subtype of int. if you think of types as sets of values, and subtypes as subsets of their parent set, that `book < int` relation suddenly makes a lot of sense ;) >>> +(not not []) 0 >>> (not not []) False >>> 0 == False True >>> 1 == True True >>> True + True 2 >>> True - False + True * 0.5 1.5 >>> isinstance(False, bool) True >>> isinstance(False, int) True >>> bool < int True so, if you accept that the operation `not []` makes sense to be defined as True (because `bool([])` is False), and that it makes sense for False to be the integer 0, then `+(not not [])` being 0 is just a logical consequence of that :) for the record, i do think it's weird for Python to define bools as ints, and to make all values define a boolean semantics via __bool__().
- dwheeler 5y agoPython was created February 1991, but it didn't have a boolean type until 2002 per PEP 285 <https://www.python.org/dev/peps/pep-0285/ https://www.python.org/dev/peps/pep-0285/> The rationale for making bool a subset of integers is for ease of implementation and substitutability (which aids backwards compatibility)47, as explained here: > In an ideal world, bool might be better implemented as a separate integer type that knows how to perform mixed-mode arithmetic. However, inheriting bool from int eases the implementation enormously (in part since all C code that calls PyInt_Check() will continue to work -- this returns true for subclasses of int). Also, I believe this is right in terms of substitutability: code that requires an int can be fed a bool and it will behave the same as 0 or 1. I have some Python code where there are still a number of uses of 0 and 1 for false and true, because it was written before Python added a boolean type.