Skip to content

Variant

The most important data type in Orblit.

In computer programming, a Variant class is a class that is designed to store a variety of other types. Dynamic programming languages like PHP, Lua and JavaScript like to use them to store variables’ data on the backend. With these Variants, properties are able to change value types freely.

// C# is statically typed. Once a variable has a type it cannot be changed. You can use the `var` keyword to let the compiler infer the type automatically.
var foo = 2; // Foo is a 32-bit integer (int).
// foo = "foo was and will always be an integer. It cannot be turned into a string!";
var boo = "Boo is a string!";
var ref = new RefCounted(); // var is especially useful when used together with a constructor.
// Orblit also provides a Variant type that works like a union of all the Variant-compatible types.
Variant fooVar = 2; // fooVar is dynamically an integer (stored as a `long` in the Variant type).
fooVar = "Now fooVar is a string!";
fooVar = new RefCounted(); // fooVar is a OrblitObject.

Orblit tracks all scripting API variables within Variants. Without even realizing it, you use Variants all the time. When a particular language enforces its own rules for keeping data typed, then that language is applying its own custom logic over the base Variant scripting API.

  • C# is statically typed, but uses its own implementation of the Variant type in place of Orblit’s Variant class when it needs to represent a dynamic value. C# Variant can be assigned any compatible type implicitly but converting requires an explicit cast.

The global @GlobalScope.typeof() function returns the enumerated value of the Variant type stored in the current variable (see Type).

Variant foo = 2;
switch (foo.VariantType)
{
case Variant.Type.Nil:
Orb.Print("foo is null");
break;
case Variant.Type.Int:
Orb.Print("foo is an integer");
break;
case Variant.Type.Object:
// Note that Objects are their own special category.
// You can convert a Variant to a OrblitObject and use reflection to get its name.
Orb.Print($"foo is a(n) {foo.AsOrblitObject().GetType().Name}");
break;
}

A Variant takes up only 20 bytes and can store almost any engine datatype inside of it. Variants are rarely used to hold information for long periods of time. Instead, they are used mainly for communication, editing, serialization and moving data around.

Orblit has specifically invested in making its Variant class as flexible as possible; so much so that it is used for a multitude of operations to facilitate communication between all of Orblit’s systems.

A Variant:

  • Can store almost any datatype.

  • Can perform operations between many variants.

  • Can be hashed, so it can be compared quickly to other variants.

  • Can be used to convert safely between datatypes.

  • Can be used to abstract calling methods and their arguments. Orblit exports all its functions through variants.

  • Can be used to defer calls or move data between threads.

  • Can be serialized as binary and stored to disk, or transferred via network.

  • Can be serialized to text and use it for printing values and editable settings.

  • Can work as an exported property, so the editor can edit it universally.

  • Can be used for dictionaries, arrays, parsers, etc.

Containers (Array and Dictionary): Both are implemented using variants. A Dictionary can match any datatype used as key to any other datatype. An Array just holds an array of Variants. Of course, a Variant can also hold a Dictionary and an Array inside, making it even more flexible.

Modifications to a container will modify all references to it. A Mutex should be created to lock it if multi-threaded access is desired.