From 97384fd5ee38e5760bfc5e028da96a3b8bb35eb9 Mon Sep 17 00:00:00 2001 From: Nathan Leiby Date: Wed, 9 Sep 2020 18:05:35 -0700 Subject: [PATCH] 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 --- .../gdscript/gdscript_styleguide.rst | 44 +++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/getting_started/scripting/gdscript/gdscript_styleguide.rst b/getting_started/scripting/gdscript/gdscript_styleguide.rst index f0aa5d798..43acfbb2f 100644 --- a/getting_started/scripting/gdscript/gdscript_styleguide.rst +++ b/getting_started/scripting/gdscript/gdscript_styleguide.rst @@ -735,11 +735,33 @@ Static typing Since Godot 3.1, GDScript supports :ref:`optional 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 ``: ``: + +:: + + var health: int = 0 + +To declare the return type of a function, use ``-> ``: + +:: + + func heal(amount: int) -> void: + +Inferred Types +~~~~~~~~~~~~~~ + +In most cases you can let the compiler infer the type, using ``:=``: + +:: + var health := 0 # The compiler will use the int type. + +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. **Good**: @@ -747,8 +769,6 @@ GDScript compiler infer the variable's type when possible. onready var health_bar: ProgressBar = get_node("UI/LifeBar") - var health := 0 # The compiler will use the int type. - **Bad**: :: @@ -756,15 +776,3 @@ GDScript compiler infer the variable's type when possible. # The compiler can't infer the exact type and will use Node # instead of ProgressBar. onready var health_bar := get_node("UI/LifeBar") - -When you let the compiler infer the type hint, write the colon and equal signs together: ``:=``. - -:: - - var health := 0 # The compiler will use the int type. - -Add a space on either sides of the return type arrow when defining functions. - -:: - - func heal(amount: int) -> void: