Browse Source

styleguide: clarify "static typing" syntax (#3687)

* styleguide: clarify "static typing" syntax

I first explored making an update here because I felt the current docs were confusing:

1. I could not tell if everything below "bad" was actually bad... It seemed like some of it was "good".
2. The examples involving ` var health := 0  # The compiler will use the int type.` seemed redundant, and re: (1) it was confusing to see one in "good" and one in "bad".

Do you agree whether a change is merited or did I misunderstand the docs as written?

---

While writing this proposed update, I realize that my explanation is less focused on style, but instead also explaining how declared and inferred types work. Is that what we expect in the style guide, or is the intent to be laser-focused it on syntax and code clarity? If so, perhaps we should avoid explanation and use the linked static typing guide as the source of truth for a rich explanation: https://docs.godotengine.org/en/stable/getting_started/scripting/gdscript/static_typing.html#doc-gdscript-static-typing).

Thanks for the awesome game engine!

* clarify why it doesn't work in the get_node() case
Nathan Leiby 4 years ago
parent
commit
97384fd5ee
1 changed files with 23 additions and 15 deletions
  1. 23 15
      getting_started/scripting/gdscript/gdscript_styleguide.rst

+ 23 - 15
getting_started/scripting/gdscript/gdscript_styleguide.rst

@@ -735,36 +735,44 @@ Static typing
 
 Since Godot 3.1, GDScript supports :ref:`optional static typing<doc_gdscript_static_typing>`.
 
-Type hints
-~~~~~~~~~~
+Declared Types
+~~~~~~~~~~~~~~
 
-Place the colon right after the variable's name, without a space, and let the
-GDScript compiler infer the variable's type when possible.
+To declare a variable's type, use ``<variable>: <type>``:
 
-**Good**:
+::
+
+   var health: int = 0
+
+To declare the return type of a function, use ``-> <type>``:
 
 ::
 
-   onready var health_bar: ProgressBar = get_node("UI/LifeBar")
+   func heal(amount: int) -> void:
 
-   var health := 0 # The compiler will use the int type.
+Inferred Types
+~~~~~~~~~~~~~~
 
-**Bad**:
+In most cases you can let the compiler infer the type, using ``:=``:
 
 ::
+   var health := 0  # The compiler will use the int type.
 
-   # The compiler can't infer the exact type and will use Node
-   # instead of ProgressBar.
-   onready var health_bar := get_node("UI/LifeBar")
+However, in a few cases when context is missing, the compiler falls back to
+the function's return type. For example, ``get_node()`` cannot infer a type
+unless the scene or file of the node is loaded in memory. In this case, you
+should set the type explicitly.
 
-When you let the compiler infer the type hint, write the colon and equal signs together: ``:=``.
+**Good**:
 
 ::
 
-   var health := 0 # The compiler will use the int type.
+   onready var health_bar: ProgressBar = get_node("UI/LifeBar")
 
-Add a space on either sides of the return type arrow when defining functions.
+**Bad**:
 
 ::
 
-   func heal(amount: int) -> void:
+   # The compiler can't infer the exact type and will use Node
+   # instead of ProgressBar.
+   onready var health_bar := get_node("UI/LifeBar")