Re: will map:put() never fail?
- Posted by euphoric (admin) Sep 09, 2011
- 1062 views
DerekParnell said...
euphoric said...
Shouldn't map:put() be a function in case it needs to report an error? I don't see anything like "map:get_error()" either...
Or maybe map:put() will never fail?
It can fail, but if it does it causes a program fail too. It can fail for these reasons ...
- The MAP ID is wrong. Either an invalid value or it refers to an object which isn't a map.
- The OPERATION argument is not a valid value.
- The OPERATION argument is invalid for the case when the KEY does not exist in the map.
- The computer's RAM has been exhausted.
I think that the idea was that if one of these situations arose, then the application is already compromised and cannot continue anyhow.
- We should return BAD_MAP_ID (-1)
- We should return BAD_OPERATION_VALUE (-2)
- We should return KEY_DOES_NOT_EXIST (-3)
- We should return OUT_OF_MEMORY (-4), if possible
Otherwise, we return 1 (SUCCESS!).
It could STILL be used as a procedure, because 4.x does not require you to use the return value. So it won't break code if we turn this into a function.
OR, we could put it on the user to test for all those things first, which is not very nice. :)