Re: Python has Traps; Does Euphoria have Programming Traps?
- Posted by jimcbrown (admin) Aug 14, 2013
- 2179 views
_tom said...
My thanks to you for developing Euphoria!
This is the first "trap" I have found in Euphoria.
The following lines execute without any warning or error messages:
? 0 <= 5 <= 9 --1 ? 0 <= -5 <= 9 --1
Ouch! Nice catch.
I think having a warning here is vital.
_tom said...
Python (and a few other languages) allow this:
Python 2.7.3 (default, Sep 26 2012, 21:53:58) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 0 <= 5 <= 9 True >>> 0 <= -5 <= 9 False >>>
The Euphoria way of writing:
? 0 <= 5 and 5 <=9 -- 1 ? 0 <= -5 and 5 <= 9 -- 0
How do I explain what is happening?
Euphoria processes expressions left to right. So in:
? 0 <= -5 <= 9
This is evaluated first:
? 0 <= -5
This evaluates to 0, naturally. Since Euphoria doesn't have a native boolean type, the integer 0 represents false and while any non-zero integer or floating point value evaluates to true, operations that return boolean values (such as =, <=, >=, et al) return the integer 1 to represent true.
So
? 0 <= -5 <= 9 -- 0 <= -5 is false, so the integer value 0 is returned -- and inserted in place of the expression ? 0 <= 9 -- Since 0 <= 9 is true, the integer value 1 is returned -- and inserted in place of the expression ? 1 --Displays 1