-
Notifications
You must be signed in to change notification settings - Fork 32
Structs
ItsDeltin edited this page Jun 25, 2021
·
10 revisions
Structs are value types that can encapsulate data. They are declared with the struct keyword.
struct Dictionary<K, V>
{
public K[] Keys;
public V[] Values;
public V Get(K key)
{
return Values[Keys.IndexOf(key)];
}
}Struct values are created by defining every properties' value.
Dictionary<Button, String> bindings = {
Keys: [Button.Interact, Button.Ultimate],
Values: ["Press Interact to place an orb.", "Press Ultimate to detonate orbs."]
};Struct types cannot be explicitly declared inline, but they can be inferred.
define point = { x: 5, y: 6 };Unlike classes, structs cannot have recursive references because it would create an infinite loop.
struct A
{
// illegal
public A one;
// illegal, allowed in programming languages such as c#
// but not here since workshop/ostw arrays are value types rather than reference types.
public A[] two;
// illegal, 'B' has a value of the 'A' type.
public B three;
// legal, since 'C' is a reference type (class) it can have a value of type 'A' and not create an infinite loop.
public C four;
}
// A struct with an A
struct B
{
public A a;
}
// A class with an A
class C
{
public A a;
}Having a struct within that same struct using anonymous types is allowed. Because the nesting is explicitly created, this will not cause an infinite loop.
struct A<T>
{
public T value;
}
A<A<Number>> doubleA = { value: { value: 5 } };