mirror of
https://github.com/godotengine/godot-docs.git
synced 2026-01-05 22:09:56 +03:00
Added changes about use of Dynamic Typing (#3045)
* Added changes about use of Dynamic Typing I have added an explanation directing doc contributors to only use Dynamic typed GDScript in their examples unless they are specifically discussing issues related to static typing.
This commit is contained in:
@@ -274,6 +274,57 @@ element of a list with more than two elements.
|
||||
How to write methods and classes
|
||||
--------------------------------
|
||||
|
||||
Dynamic vs Static typing
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
GDScript aims to be concise and easy to learn. Optional static typing conflicts with this vision
|
||||
by making documentation more congested. To improve its accessibility for new users, please use
|
||||
dynamic GDScript code samples where possible. The one exception is topics that explain
|
||||
static typing concepts to users.
|
||||
|
||||
**Don't** add a type hint with a colon or by casting:
|
||||
|
||||
::
|
||||
|
||||
const main_attack := preload("res://fire_attack.gd")
|
||||
var hit_points := 5
|
||||
var name: String = "Bob"
|
||||
var body_sprite := $Sprite as Sprite
|
||||
|
||||
|
||||
**Do** write constants variables with dynamic typing:
|
||||
|
||||
::
|
||||
|
||||
const main_attack = preload("res://fire_attack.gd")
|
||||
var hit_points = 5
|
||||
var name = "Bob"
|
||||
var body_sprite = $Sprite
|
||||
|
||||
|
||||
**Don't** write functions with inferred arguments or return types:
|
||||
|
||||
::
|
||||
|
||||
func choose(arguments: Array):
|
||||
# Chooses one of the arguments from array with equal chance
|
||||
randomize()
|
||||
var size := arguments.size()
|
||||
var choice: int = randi() % size
|
||||
return arguments[choice]
|
||||
|
||||
**Do** write functions using dynamic typing:
|
||||
|
||||
::
|
||||
|
||||
func choose(arguments:):
|
||||
# Chooses one of the arguments from array with equal chance
|
||||
randomize()
|
||||
var size = arguments.size()
|
||||
var choice = randi() % size
|
||||
return arguments[choice]
|
||||
|
||||
|
||||
Give an overview of the node in the brief description
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
Reference in New Issue
Block a user