From 5a46a02f4b5f40f0718a17a4fec31d71dade46b4 Mon Sep 17 00:00:00 2001 From: Lexyth Date: Wed, 19 Mar 2025 00:42:37 +0100 Subject: [PATCH 1/3] Fix misplaced bad example Moved the bad example for the inferred type for functions that return super types directly after its related statement. --- .../scripting/gdscript/gdscript_styleguide.rst | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tutorials/scripting/gdscript/gdscript_styleguide.rst b/tutorials/scripting/gdscript/gdscript_styleguide.rst index 6230736eab4..60d5f2d8acc 100644 --- a/tutorials/scripting/gdscript/gdscript_styleguide.rst +++ b/tutorials/scripting/gdscript/gdscript_styleguide.rst @@ -1032,6 +1032,16 @@ 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. +**Bad**: + +.. rst-class:: code-example-bad + +:: + + # The compiler can't infer the exact type and will use Node + # instead of ProgressBar. + @onready var health_bar := get_node("UI/LifeBar") + **Good**: .. rst-class:: code-example-good @@ -1052,12 +1062,4 @@ that type will be used to infer the type of the var. This option is also considered more :ref:`type-safe` than the first. -**Bad**: -.. rst-class:: code-example-bad - -:: - - # The compiler can't infer the exact type and will use Node - # instead of ProgressBar. - @onready var health_bar := get_node("UI/LifeBar") From 014031046a1f2e0447811e08d32f62e944ed8dbf Mon Sep 17 00:00:00 2001 From: Lexyth Date: Sun, 23 Mar 2025 11:08:24 +0100 Subject: [PATCH 2/3] Fix good/bad example order Moved the bad example below the good example to stay consistent with previous instances. --- .../scripting/gdscript/gdscript_styleguide.rst | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tutorials/scripting/gdscript/gdscript_styleguide.rst b/tutorials/scripting/gdscript/gdscript_styleguide.rst index 60d5f2d8acc..d7d5bf031c3 100644 --- a/tutorials/scripting/gdscript/gdscript_styleguide.rst +++ b/tutorials/scripting/gdscript/gdscript_styleguide.rst @@ -1032,27 +1032,29 @@ 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. -**Bad**: +**Good**: -.. rst-class:: code-example-bad +.. rst-class:: code-example-good :: - # The compiler can't infer the exact type and will use Node - # instead of ProgressBar. - @onready var health_bar := get_node("UI/LifeBar") + @onready var health_bar: ProgressBar = get_node("UI/LifeBar") -**Good**: +**Bad**: -.. rst-class:: code-example-good +.. rst-class:: code-example-bad :: - @onready var health_bar: ProgressBar = get_node("UI/LifeBar") + # The compiler can't infer the exact type and will use Node + # instead of ProgressBar. + @onready var health_bar := get_node("UI/LifeBar") Alternatively, you can use the ``as`` keyword to cast the return type, and that type will be used to infer the type of the var. +**Good**: + .. rst-class:: code-example-good :: From 4b04e656cde5f32f3ec3e0075084a46ff8b83410 Mon Sep 17 00:00:00 2001 From: Lexyth Date: Sun, 23 Mar 2025 11:11:58 +0100 Subject: [PATCH 3/3] Remove Good example title from as keyword example Removed Good example title from the example for the "as" keyword, as it is a neutral example. --- tutorials/scripting/gdscript/gdscript_styleguide.rst | 2 -- 1 file changed, 2 deletions(-) diff --git a/tutorials/scripting/gdscript/gdscript_styleguide.rst b/tutorials/scripting/gdscript/gdscript_styleguide.rst index d7d5bf031c3..ccbdb90d738 100644 --- a/tutorials/scripting/gdscript/gdscript_styleguide.rst +++ b/tutorials/scripting/gdscript/gdscript_styleguide.rst @@ -1053,8 +1053,6 @@ should set the type explicitly. Alternatively, you can use the ``as`` keyword to cast the return type, and that type will be used to infer the type of the var. -**Good**: - .. rst-class:: code-example-good ::