CompiledFrog

Post #53: GDScript Variant Types

GDScript uses Variant types.

Variant types are types in a programming language that are built to store a "variety" of other types. For example, here is the code snippet from the Godot Variant type reference page (link above):

var foo = 2 # foo is dynamically an integer
foo = "Now foo is a string!"
foo = RefCounted.new() # foo is an Object
var bar: int = 2 # bar is a statically typed integer.
# bar = "Uh oh! I can't make statically typed variables become a different type!"

This allows foo to be changed, and the available ways of interacting with the type change depending on the actual underlying type stored within. For example, trying to get the length of the string foo might not make sense (becoming no longer allowed) once the value stored within is not a string.

Other languages handle this differently. For example, Rust uses variable shadowing on declaration of a new variable using the same name.

There are definitely pros and cons to GDScript's approach, as is true across programming languages for many various features!