comp.lang.idl-pvwave archive - RDF feed
type: channel
description: Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado
items: Using bitwise OR with non-integers; and using !NULL in expressions

Using bitwise OR with non-integers; and using !NULL in expressions
type: item
description: Hello all, I found to my delight that bitwise OR on strings evaluates left-to-right, with empty strings counting as FALSE, which lets me (partially) adapt a useful idiom for default values from some other language: my_value = table_value OR default_value which to my mind is much more readable than (and avoids the repetition of) the equivalent: my_value = table_value ? table_value : default_value or (horror of horrors): IF table_value NE '' THEN BEGIN my_value = table_value ENDIF ELSE BEGIN my_value = default_value ENDELSE It is perhaps strange that it should work, but I was happy to discover this. Trying to see how far this useful idiom could be stretched, I tried a couple of other variants: IDL> print, 0.0 or 42.0 42.0000 IDL> print, 1.2 or 42.0 1.20000 So the idiom carries over to floating-point numbers also. Excellent. IDL> print, 1 or 42.0 1.00000 Huh? Why did my integer become a float? IDL> print, complex(0,0) or 42 ( 42.0000, 0.00000) Okay, so IDL converts both sides to the narrowest type wide enough to accomodate both sides. If you're going to do bitwise OR, this makes sense. If you're going to return one side or the other, it is less useful, IMO. IDL> print, 0 or 'foo' % Type conversion error: Unable to convert given STRING to Long. % Detected at: $MAIN$ 0 Well, for an operator advertised to do bitwise OR on integers, trying to convert string to integer is perhaps not unreasonable. IDL> print, !NULL or 'foo' % Variable is undefined: <UNDEFINED>. % Execution halted at: $MAIN$ But in this case converting the type is a bad idea: !NULL is false, just like the empty string is, and I would have hoped that this should evaluate to 'foo' without error. The same goes for this case: IDL> print, 'foo' or !NULL % Variable is undefined: <UNDEFINED>. % Execution halted at: $MAIN$ What I'd like to see (as a minimal change from today's behaviour) is logic something like this: IF <both sides are integer types> THEN BEGIN RETURN, <bitwise or in widest type necessary> ENDIF ELS…
creator: tom.grydeland
date: 2012-11-28T10:13:09-00:00