Static Typing Etiquette (Casting and Safe Line)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mrfatalo

Hi I’m new as a programmer, I apologize if the question may seem stupid or repetitive.
//
Question about compiling the code in static typing:
Am I writing incorrectly? (It’s works, but can cause trouble, mistakes, confusions?)
//
This is my example:
enter image description here
enter image description here
enter image description here

I know i can use the := operator on variables.
(but I find it annoying to go to the end of the line to read the type)
I know aswell i can store variables inside the function():.
(And I also find this annoying because sometimes it becomes a longer line of variables)
What is the right solution?
Did i thinking, and making, wrong?
Thank you all for you attention, sorry again if question and code looking stupid.

:bust_in_silhouette: Reply From: andrej88

On lines 15–20, you don’t need the as CanvasItem parts. Using as casts the left-hand-side into the type on the right (see here for examples). Your code doesn’t need to cast because it already knows the variables are all of the type CanvasItem, because that’s what it says on lines 5–10.

Also, one minor thing: it’s more common to write variable declarations like this

var my_var: String

instead of like this

var my_var : String

The GDScript style guide uses the first style, and other languages with a similar type syntax do the same (Kotlin, Swift, Rust, Python).

Thank you very much for your answer, i will follow your advice to fix the code!
I’m very interested in all the minor thing adjustments for improving.

mrfatalo | 2022-08-07 14:02