_overloadMinus: what to do with invalid input? [message #89928] |
Mon, 29 December 2014 10:34  |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
Hello,
I am overloading the "-" operator for various objects and I have a
philosophical question about what to do when the objects do not match.
For example, should one return a FALSE result if the objects are not the
same, e.g.
FUNCTION Cloud::_overloadMinus, left, right
IF ( (~ ISA(left,'Cloud')) || (~ ISA(right,'Cloud')) ) THEN $
RETURN, FALSE
giving:
IDL> q = fd_cloud[0] - 1
IDL> help, q
Q INT = 0
...or should one throw an error an halt, e.g.
FUNCTION Cloud::_overloadMinus, left, right
IF ( (~ ISA(left,'Cloud')) || (~ ISA(right,'Cloud')) ) THEN $
MESSAGE, 'Must supply two Cloud objects for subtraction'
leading to:
IDL> q = fd_cloud[0] - 1
% CLOUD::_OVERLOADMINUS: Must supply two Cloud objects for subtraction
Which is the more idiomatic for IDL?
cheers,
paulv
|
|
|
Re: _overloadMinus: what to do with invalid input? [message #89929 is a reply to message #89928] |
Mon, 29 December 2014 11:41   |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
Paul van Delst <paul.vandelst@noaa.gov> wrote:
> Hello,
>
> I am overloading the "-" operator for various objects and I have a
> philosophical question about what to do when the objects do not match.
>
> For example, should one return a FALSE result if the objects are not the same, e.g.
>
> FUNCTION Cloud::_overloadMinus, left, right
> IF ( (~ ISA(left,'Cloud')) || (~ ISA(right,'Cloud')) ) THEN $
> RETURN, FALSE
>
> giving:
>
>> q = fd_cloud[0] - 1
>> help, q
> Q INT = 0
>
>
> ...or should one throw an error an halt, e.g.
>
>
> FUNCTION Cloud::_overloadMinus, left, right
> IF ( (~ ISA(left,'Cloud')) || (~ ISA(right,'Cloud')) ) THEN $
> MESSAGE, 'Must supply two Cloud objects for subtraction'
>
> leading to:
>
>> q = fd_cloud[0] - 1
> % CLOUD::_OVERLOADMINUS: Must supply two Cloud objects for subtraction
>
>
> Which is the more idiomatic for IDL?
>
> cheers,
>
> paulv
I would think an error and halting, like IDL would do if you tried to use
an invalid operator with the native types. What happens if you try to add
two pointers? (not in front of my computer right now)
Mike
--
www.michaelgalloy.com
Research Mathematician
Tech-X Corporation
|
|
|
Re: _overloadMinus: what to do with invalid input? [message #89931 is a reply to message #89929] |
Mon, 29 December 2014 14:27   |
penteado
Messages: 866 Registered: February 2018
|
Senior Member Administrator |
|
|
On Monday, December 29, 2014 5:41:49 PM UTC-2, Mike Galloy wrote:
> I would think an error and halting, like IDL would do if you tried to use
> an invalid operator with the native types. What happens if you try to add
> two pointers? (not in front of my computer right now)
To answer Mike's question:
IDL> a=ptr_new(1)
IDL> b=ptr_new(2)
IDL> c=a+b
% Operation illegal with pointer types.
% Execution halted at: $MAIN$
Regarding Paul's question, I would say that the answer depends on how one envisions the object's usage. If one decides it makes no sense to do the subtraction, like with pointers, it should throw an error. An error should also be raised, instead of returning a value, if such a return value could be confused with a valid result. For instance, taking IDL's list:
IDL> l=list(1,2)
IDL> l+3
% LIST::_OVERLOADPLUS: Arguments must both be lists.
% Execution halted at: $MAIN$
IDL> l-1
% Unable to convert variable to type object reference.
% Execution halted at: $MAIN$
It throws an error if I try to add or subtract an integer to a list. If it returned something, like !null, 0 or an empty list, such a return could be confused with something that is a valid result, obtained from different operations.
One could argue that adding/subtracting a scalar to a list should mean applying that operation to each element in the list, so that l-1 above should be equivalent to l.map(lambda(x:x-1)). But that is not what currently happens (as of IDL 8.4).
|
|
|
Re: _overloadMinus: what to do with invalid input? [message #89932 is a reply to message #89931] |
Tue, 30 December 2014 06:58  |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
Hello,
On 12/29/14 17:27, Paulo Penteado wrote:
> On Monday, December 29, 2014 5:41:49 PM UTC-2, Mike Galloy wrote:
>> I would think an error and halting, like IDL would do if you tried to use
>> an invalid operator with the native types. What happens if you try to add
>> two pointers? (not in front of my computer right now)
>
> To answer Mike's question:
>
> IDL> a=ptr_new(1)
> IDL> b=ptr_new(2)
> IDL> c=a+b
> % Operation illegal with pointer types.
> % Execution halted at: $MAIN$
>
> Regarding Paul's question, I would say that the answer depends on how
> one envisions the object's usage. If one decides it makes no sense to
> do the subtraction, like with pointers, it should throw an error. An
> error should also be raised, instead of returning a value, if such a
> return value could be confused with a valid result. For instance,
> taking IDL's list:
>
> IDL> l=list(1,2)
> IDL> l+3
> % LIST::_OVERLOADPLUS: Arguments must both be lists.
> % Execution halted at: $MAIN$
Oh. That make sense. I guess I should have tested using objects too,
rather than intrinsic types.
An error it is.
Thanks guys.
cheers,
paulv
|
|
|