Struct¶
Introduction¶
Struct type is a collection of fields, it allows you to give more meaning to your data. In layman terms, you can imagine struct as mould.
To create a structure in Pineapple, you have to use the def
keyword.
Tip
In Pineapple, every field name must starts with the colon :
symbol.
For example,
1 2 3 | def People :name String :salary Number |
Note
People
is the struct name, while :name
and :salary
is the field name.
To create new data from your struct, you have to use the struct name.
1 2 3 | let john = People :name = "John" :salary = 999 |
Note that the indentation for each field is necessary.
To access the field data:
1 | let x = john:name |
Recursive struct¶
You can also create recursive struct which contain fields that points to itself.
For example:
1 2 3 | def People :name String :friend People? |
And here's how you create new data from it:
1 2 3 4 5 6 7 | let john = People :name = "Marry" :friend = People :name = "Jane" :friend = People :name = "Lee" :friend = `nil |
Accessing data:
1 | let acquaintance = john:friend:friend |
Generic struct¶
You can also create generic structure in Pineapple, this feature is important when you need to create custom data structures.
Generic struct can help you to prevent some silly type error.
For example,
1 2 3 4 5 6 7 8 9 10 | // here's how you declare a generic struct def Node{T} :current T :next Node{T}? // here's how you use it def .main let x = Node{Integer} :current = "10" // Error, should be Integer, not String :next = `nil |