From 8834d1031500185970692beb7a9adc98c4235e3d Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Tue, 16 Feb 2021 00:02:07 +0100 Subject: [PATCH] docs: add structs --- docs/SUMMARY.md | 1 + docs/concepts/structured-data.md | 66 ++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 docs/concepts/structured-data.md diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 361de59..0eca749 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -9,6 +9,7 @@ - [Functions](./concepts/functions.md) - [Comments](./concepts/comments.md) - [Control Flow](./concepts/control-flow.md) + - [Structured data](./concepts/structured-data.md) - [Developer Resources](./developers/SUMMARY.md) - [Contributing to Sabre](./developers/contributing.md) - [Compiler Backends](./developers/backends.md) diff --git a/docs/concepts/structured-data.md b/docs/concepts/structured-data.md new file mode 100644 index 0000000..705ea09 --- /dev/null +++ b/docs/concepts/structured-data.md @@ -0,0 +1,66 @@ +# Structured data + +When working with data, you often find yourself needing to group information together. This is where a `struct` could come into play. A _struct_, or _structure_, is a custom data type that lets you name and package together multiple related values that make up a meaningful group. If you’re familiar with an object-oriented language, a struct is like an object’s data attributes. + +## Defining structs + +To define a struct, we enter the keyword `struct` and name the entire struct. A struct’s name should describe the significance of the pieces of data being grouped together. Then, inside curly brackets, we define the names and types of the pieces of data, which we call fields. The following example shows a struct that stores information about a user account. + +``` +struct User { + username: string, + email: string, + sign_in_count: int, + active: bool, +} +``` + +Structs can be nested as a type inside other structs. For example, we could assign each user an address, which itself is a struct. + +``` +struct Address { + street: string, + number: int + postal_code: string, + city: string +} + +struct User { + username: string, + email: string, + address: Address +} +``` + +## Instantiating structs + +To use a struct after we’ve defined it, we create an _instance_ of that struct by specifying concrete values for each of the fields. We create an instance by stating the name of the struct and then add curly brackets containing `key: value` pairs, where the keys are the names of the fields and the values are the data we want to store in those fields. We don’t have to specify the fields in the same order in which we declared them in the struct. In other words, the struct definition is like a general template for the type, and instances fill in that template with particular data to create values of the type. Let's use our `User` struct from a previous example and create an user called `alice`. + +``` +struct User { + username: string, + email: string, + sign_in_count: int, + active: bool, +} + +let alice = new User { + email: "alice@example.com", + username: "alice", + sign_in_count: 1, + active: true +} +``` + +To get a specific value from a struct, we can use dot notation. If we wanted just alice's email address, we could use `alice.email` wherever we wanted to use this value. Fields of structs can also be reassigned using the dot notation: + +``` +let alice = new User { + email: "alice@example.com", + username: "alice", + sign_in_count: 1, + active: true +} + +alice.sign_in_count = 2 +```