setd_default
| Definition: | setd_default(object o, integer tid) |
| Description: |
Override the default result (initially NULL) for getd() when a key is not found.
o: the new default value, can be any valid object. tid: a result from new_dict() - nb the usual default of 1 is omitted. |
| pwa/p2js: | Supported. |
| Comments: |
Since it is legal to store any valid object as the data, there may be no default that unequivocably means
"missing key" when returned by getd(), in which case you must test whether the
result of getd_index() is zero instead.
In other words this routine is only useful when the existing default of 0 is getting in the way, but you have a valid/better alternative, that will never clash with anything else that you might store. The default can only be set per-dictionary (ideally as created), as obviously any blanket alterations to the defaults could easily cripple otherwise perfectly working third party components that rely on NULL. Likewise changing the default on dictionary 1 will obviously cause problems if any other component in a large application either also invokes setd_default on that with a different o, or tests results for NULL. Hence in this one particular case the usual default of 1 for tid has been deliberately omitted. While it remains legal to explicitly specify 1, ideally that should only be done in trivial/throw-away programs. |
| Example: |
integer patterns = new_dict()
?getd("?",patterns) -- prints 0
setd_default(-1,patterns)
?getd("?",patterns) -- prints -1
|
| See Also: | new_dict, getd, getd_index |