Document the `in` operator in String, Array and Dictionary classes

This also clarifies the `in` operator behavior in Object.

(cherry picked from commit 7f01f68226)
This commit is contained in:
Hugo Locurcio 2020-06-11 19:53:55 +02:00 committed by Rémi Verschelde
parent 50c4834548
commit 5a1f94ba47
4 changed files with 26 additions and 6 deletions

View File

@ -176,7 +176,7 @@
<argument index="1" name="from" type="int" default="0"> <argument index="1" name="from" type="int" default="0">
</argument> </argument>
<description> <description>
Searches the array for a value and returns its index or -1 if not found. Optionally, the initial search index can be passed. Searches the array for a value and returns its index or [code]-1[/code] if not found. Optionally, the initial search index can be passed.
</description> </description>
</method> </method>
<method name="find_last"> <method name="find_last">
@ -185,7 +185,7 @@
<argument index="0" name="value" type="Variant"> <argument index="0" name="value" type="Variant">
</argument> </argument>
<description> <description>
Searches the array in reverse order for a value and returns its index or -1 if not found. Searches the array in reverse order for a value and returns its index or [code]-1[/code] if not found.
</description> </description>
</method> </method>
<method name="front"> <method name="front">
@ -208,6 +208,12 @@
["inside", 7].has(7) == true ["inside", 7].has(7) == true
["inside", 7].has("7") == false ["inside", 7].has("7") == false
[/codeblock] [/codeblock]
[b]Note:[/b] This is equivalent to using the [code]in[/code] operator as follows:
[codeblock]
# Will evaluate to `true`.
if 2 in [2, 4, 6, 8]:
pass
[/codeblock]
</description> </description>
</method> </method>
<method name="hash"> <method name="hash">

View File

@ -124,6 +124,13 @@
</argument> </argument>
<description> <description>
Returns [code]true[/code] if the dictionary has a given key. Returns [code]true[/code] if the dictionary has a given key.
[b]Note:[/b] This is equivalent to using the [code]in[/code] operator as follows:
[codeblock]
# Will evaluate to `true`.
if "godot" in {"godot": "engine"}:
pass
[/codeblock]
This method (like the [code]in[/code] operator) will evaluate to [code]true[/code] as long as the key exists, even if the associated value is [code]null[/code].
</description> </description>
</method> </method>
<method name="has_all"> <method name="has_all">

View File

@ -15,6 +15,7 @@
print("position" in n) # Prints "True". print("position" in n) # Prints "True".
print("other_property" in n) # Prints "False". print("other_property" in n) # Prints "False".
[/codeblock] [/codeblock]
The [code]in[/code] operator will evaluate to [code]true[/code] as long as the key exists, even if the value is [code]null[/code].
Objects also receive notifications. Notifications are a simple way to notify the object about different events, so they can all be handled together. See [method _notification]. Objects also receive notifications. Notifications are a simple way to notify the object about different events, so they can all be handled together. See [method _notification].
</description> </description>
<tutorials> <tutorials>

View File

@ -338,7 +338,13 @@
<argument index="1" name="from" type="int" default="0"> <argument index="1" name="from" type="int" default="0">
</argument> </argument>
<description> <description>
Finds the first occurrence of a substring. Returns the starting position of the substring or -1 if not found. Optionally, the initial search index can be passed. Finds the first occurrence of a substring. Returns the starting position of the substring or [code]-1[/code] if not found. Optionally, the initial search index can be passed.
[b]Note:[/b] If you just want to know whether a string contains a substring, use the [code]in[/code] operator as follows:
[codeblock]
# Will evaluate to `false`.
if "i" in "team":
pass
[/codeblock]
</description> </description>
</method> </method>
<method name="find_last"> <method name="find_last">
@ -347,7 +353,7 @@
<argument index="0" name="what" type="String"> <argument index="0" name="what" type="String">
</argument> </argument>
<description> <description>
Finds the last occurrence of a substring. Returns the starting position of the substring or -1 if not found. Finds the last occurrence of a substring. Returns the starting position of the substring or [code]-1[/code] if not found.
</description> </description>
</method> </method>
<method name="findn"> <method name="findn">
@ -358,7 +364,7 @@
<argument index="1" name="from" type="int" default="0"> <argument index="1" name="from" type="int" default="0">
</argument> </argument>
<description> <description>
Finds the first occurrence of a substring, ignoring case. Returns the starting position of the substring or -1 if not found. Optionally, the initial search index can be passed. Finds the first occurrence of a substring, ignoring case. Returns the starting position of the substring or [code]-1[/code] if not found. Optionally, the initial search index can be passed.
</description> </description>
</method> </method>
<method name="format"> <method name="format">
@ -860,7 +866,7 @@
<argument index="1" name="len" type="int" default="-1"> <argument index="1" name="len" type="int" default="-1">
</argument> </argument>
<description> <description>
Returns part of the string from the position [code]from[/code] with length [code]len[/code]. Argument [code]len[/code] is optional and using -1 will return remaining characters from given position. Returns part of the string from the position [code]from[/code] with length [code]len[/code]. Argument [code]len[/code] is optional and using [code]-1[/code] will return remaining characters from given position.
</description> </description>
</method> </method>
<method name="to_ascii"> <method name="to_ascii">