Page 1 of 1

[not a bug] Docs dictionary bug

Posted: Sun Nov 09, 2008 1:00 am
by Targun
http://docs.polserver.com/pol097/objref.php#Dictionary
states that dictionary.exists(key) returns: error/int if exists, returns count of matching key

it's not true - this method returns boolean: 1 if key exists 0 if not.

Re: Docs dictionary bug

Posted: Sun Nov 09, 2008 3:12 pm
by Luth
Both you and the documentation are correct. Because dictionaries are maps, there can only be zero or one matching entry in the dictionary for any key.

Code: Select all

var d := dictionary;
d["apple"] := "one";
d["banana"] := "two";
d["cookie"] := "three";
	
print("exists: "+d.exists("apple"));
print("exists: "+d.exists("banana"));
print("exists: "+d.exists("cookie"));
print("exists: "+d.exists("dogfood"));
will result in:

Code: Select all

exists: 1
exists: 1
exists: 1
exists: 0
Why? Because there is exactly 1 key of "apple" in the dictionary, 1 key of "banana", 1 key of "cookie", and 0 keys of "dogfood."

8)

Re: Docs dictionary bug

Posted: Tue Nov 11, 2008 1:45 am
by Targun
well, maybe it's my poor english skill, but "returns: error/int" and "if exists, returns count of matching key" made me think, that method returns integer only if key exists and error if it doesnt exist.

Re: Docs dictionary bug

Posted: Tue Nov 11, 2008 1:05 pm
by Luth
Aaah. I can see how that could be worded better. "error" is typically reserved for actual errors. Since testing for whether a key exists even when it doesn't is not an error, but in fact exactly what it's intended for, it returns 0. "error" is returned for invalid parameters.