Structures
#
StructuresStructure is a custom type which contains complex data (or no data). It can be described as a simple key-value storage where key is a name of property and value is what's stored. Defined using keyword struct
. Struct can have up to 4 abilities, they are specified with type definition.
Struct is the only way to create custom type in Rogue.
#
DefinitionStruct definition is only allowed inside a method. It begins with keyword struct
, followed by name and curly braces, where struct fields are defined:
Look at these examples of struct definitions:
Max number of fields in one struct is 65535.
Every defined struct becomes new a type. This type can be accessed through its method (just like you would access method functions):
#
Recursive definitionShort as never:
Recursive struct definition is impossible.
You are allowed to use another struct as type but you can't recursively use the same struct. Rogue compiler checks recursive definitions and won't let you compile code like this:
#
Create new structTo use this type you need to create its instance.
New instances can only be created inside the method where they're defined.
To create new instance use it's definition, but instead of passing types pass values of these types:
Rogue also allows you to create new instances shorter - by passing variable name which matches struct's field (and type!). We can simplify our new_country()
method using this rule:
To create an empty struct (with no fields) simply use curly braces:
#
Access struct fieldsStructs would have been almost useless if we hadn't had a way to access their fields (though you can create struct without fields).
Only method can access its struct's fields. Outside of method fields are private.
Struct fields are only visible inside its method. Outside of this method (in script or another method) it's just a type. To access struct's fields use .
(dot) notation:
If nested struct type is defined in the same method it can be accessed in similar manner which can be generally described as:
#
Destructing structuresTo destruct a struct use let <STRUCT DEF> = <STRUCT>
syntax:
You should remember that unused variables are prohibited in Rogue and sometimes you may need to destruct a structure without using its fields. For unused struct fields use _
- underscore:
Destructuring may not seem important right now. But remember it - it will play a huge part when we get to resources.
#
Implementing getter-functions for struct fieldsTo make struct fields readable outside, you need to implement methods which will read these fields and pass them as return values. Usually the getter method is called the same way as struct's field but it may cause inconvenience if your method defines more than one struct.
By making getters we've allowed method users access fields of our struct:
Now you know how to define custom type - struct, but by default its functionality is limited. In the next chapter you will learn about abilities - a way to define how values of this type can be manipulated and used.