From ccf75d2c362ac8e4dcd12c781f6e1eafd0064813 Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Tue, 26 Mar 2024 11:56:33 +0000 Subject: Document import type Closes https://github.com/gleam-lang/language-tour/issues/61 --- .../lesson14_type_aliases/code.gleam | 12 ----------- .../chapter0_basics/lesson14_type_aliases/en.html | 12 ----------- .../lesson14_type_imports/code.gleam | 10 ++++++++++ .../chapter0_basics/lesson14_type_imports/en.html | 17 ++++++++++++++++ .../chapter0_basics/lesson15_blocks/code.gleam | 13 ------------ .../chapter0_basics/lesson15_blocks/en.html | 23 ---------------------- .../lesson15_type_aliases/code.gleam | 12 +++++++++++ .../chapter0_basics/lesson15_type_aliases/en.html | 12 +++++++++++ .../chapter0_basics/lesson16_blocks/code.gleam | 13 ++++++++++++ .../chapter0_basics/lesson16_blocks/en.html | 23 ++++++++++++++++++++++ .../chapter0_basics/lesson16_lists/code.gleam | 16 --------------- src/content/chapter0_basics/lesson16_lists/en.html | 19 ------------------ .../chapter0_basics/lesson17_constants/code.gleam | 13 ------------ .../chapter0_basics/lesson17_constants/en.html | 18 ----------------- .../chapter0_basics/lesson17_lists/code.gleam | 16 +++++++++++++++ src/content/chapter0_basics/lesson17_lists/en.html | 19 ++++++++++++++++++ .../chapter0_basics/lesson18_constants/code.gleam | 13 ++++++++++++ .../chapter0_basics/lesson18_constants/en.html | 18 +++++++++++++++++ 18 files changed, 153 insertions(+), 126 deletions(-) delete mode 100644 src/content/chapter0_basics/lesson14_type_aliases/code.gleam delete mode 100644 src/content/chapter0_basics/lesson14_type_aliases/en.html create mode 100644 src/content/chapter0_basics/lesson14_type_imports/code.gleam create mode 100644 src/content/chapter0_basics/lesson14_type_imports/en.html delete mode 100644 src/content/chapter0_basics/lesson15_blocks/code.gleam delete mode 100644 src/content/chapter0_basics/lesson15_blocks/en.html create mode 100644 src/content/chapter0_basics/lesson15_type_aliases/code.gleam create mode 100644 src/content/chapter0_basics/lesson15_type_aliases/en.html create mode 100644 src/content/chapter0_basics/lesson16_blocks/code.gleam create mode 100644 src/content/chapter0_basics/lesson16_blocks/en.html delete mode 100644 src/content/chapter0_basics/lesson16_lists/code.gleam delete mode 100644 src/content/chapter0_basics/lesson16_lists/en.html delete mode 100644 src/content/chapter0_basics/lesson17_constants/code.gleam delete mode 100644 src/content/chapter0_basics/lesson17_constants/en.html create mode 100644 src/content/chapter0_basics/lesson17_lists/code.gleam create mode 100644 src/content/chapter0_basics/lesson17_lists/en.html create mode 100644 src/content/chapter0_basics/lesson18_constants/code.gleam create mode 100644 src/content/chapter0_basics/lesson18_constants/en.html (limited to 'src') diff --git a/src/content/chapter0_basics/lesson14_type_aliases/code.gleam b/src/content/chapter0_basics/lesson14_type_aliases/code.gleam deleted file mode 100644 index 6125ffe..0000000 --- a/src/content/chapter0_basics/lesson14_type_aliases/code.gleam +++ /dev/null @@ -1,12 +0,0 @@ -import gleam/io - -pub type UserId = - Int - -pub fn main() { - let one: UserId = 1 - let two: Int = 2 - - // UserId and Int are the same type - io.debug(one == two) -} diff --git a/src/content/chapter0_basics/lesson14_type_aliases/en.html b/src/content/chapter0_basics/lesson14_type_aliases/en.html deleted file mode 100644 index 9fddd70..0000000 --- a/src/content/chapter0_basics/lesson14_type_aliases/en.html +++ /dev/null @@ -1,12 +0,0 @@ -

- A type alias can be used to refer to a type by a different name. Giving a type - an alias doesn't make a new type, it is still the same type. -

-

- A type's name always starts with a capital letter, contrasting to variables - and functions, which start with a lowercase letter. -

-

- When the pub keyword is used the type alias is public and can be - referred to by other modules. -

diff --git a/src/content/chapter0_basics/lesson14_type_imports/code.gleam b/src/content/chapter0_basics/lesson14_type_imports/code.gleam new file mode 100644 index 0000000..61f552a --- /dev/null +++ b/src/content/chapter0_basics/lesson14_type_imports/code.gleam @@ -0,0 +1,10 @@ +import gleam/bytes_builder +import gleam/string_builder.{type StringBuilder} + +pub fn main() { + // Referring to a type in a qualified way + let _bytes: bytes_builder.BytesBuilder = bytes_builder.new() + + // Refering to a type in an unqualified way + let _text: StringBuilder = string_builder.new() +} diff --git a/src/content/chapter0_basics/lesson14_type_imports/en.html b/src/content/chapter0_basics/lesson14_type_imports/en.html new file mode 100644 index 0000000..84f18a5 --- /dev/null +++ b/src/content/chapter0_basics/lesson14_type_imports/en.html @@ -0,0 +1,17 @@ +

+ Other modules may also define types that we wish to refer to. In this case we + need to import them. +

+

+ Like functions types can be referred to in a qualified way by putting + the imported module name and a dot before the type name. For example, + bytes_builder.BytesBuilder +

+

+ Types can also be imported in an unqualified way by listing them in + the import statement with the word type before the type name. +

+

+ It is more common in Gleam code for types to be imported in an unqualified way + than it is for funtions to be imported in an unqualified way. +

diff --git a/src/content/chapter0_basics/lesson15_blocks/code.gleam b/src/content/chapter0_basics/lesson15_blocks/code.gleam deleted file mode 100644 index 31e4729..0000000 --- a/src/content/chapter0_basics/lesson15_blocks/code.gleam +++ /dev/null @@ -1,13 +0,0 @@ -import gleam/io - -pub fn main() { - let fahrenheit = { - let degrees = 64 - degrees - } - // io.debug(degrees) // <- This will not compile - - // Changing order of evaluation - let celsius = { fahrenheit - 32 } * 5 / 9 - io.debug(celsius) -} diff --git a/src/content/chapter0_basics/lesson15_blocks/en.html b/src/content/chapter0_basics/lesson15_blocks/en.html deleted file mode 100644 index bc82e39..0000000 --- a/src/content/chapter0_basics/lesson15_blocks/en.html +++ /dev/null @@ -1,23 +0,0 @@ -

- Blocks are one or more expressions grouped together with curly braces. Each - expression is evaluated in order and the value of the last expression is - returned. -

-

- Any variables assigned within the block can only be used within the block. -

-

- Try uncommenting io.debug(degrees) to see the compile error from - trying to use a variable that is not in scope. -

-

- Blocks can also be used to change the order of evaluation of binary operators - expressions. -

-

- * binds more tightly than + so the expression - 1 + 2 * 3 evaluates to 7. If the 1 + 2 should be - evaluated first to make the expression evaluate to 9 then the expression can be - wrapped in a block: { 1 + 2 } * 3. This is similar to grouping - with parentheses in some other languages. -

diff --git a/src/content/chapter0_basics/lesson15_type_aliases/code.gleam b/src/content/chapter0_basics/lesson15_type_aliases/code.gleam new file mode 100644 index 0000000..6125ffe --- /dev/null +++ b/src/content/chapter0_basics/lesson15_type_aliases/code.gleam @@ -0,0 +1,12 @@ +import gleam/io + +pub type UserId = + Int + +pub fn main() { + let one: UserId = 1 + let two: Int = 2 + + // UserId and Int are the same type + io.debug(one == two) +} diff --git a/src/content/chapter0_basics/lesson15_type_aliases/en.html b/src/content/chapter0_basics/lesson15_type_aliases/en.html new file mode 100644 index 0000000..9fddd70 --- /dev/null +++ b/src/content/chapter0_basics/lesson15_type_aliases/en.html @@ -0,0 +1,12 @@ +

+ A type alias can be used to refer to a type by a different name. Giving a type + an alias doesn't make a new type, it is still the same type. +

+

+ A type's name always starts with a capital letter, contrasting to variables + and functions, which start with a lowercase letter. +

+

+ When the pub keyword is used the type alias is public and can be + referred to by other modules. +

diff --git a/src/content/chapter0_basics/lesson16_blocks/code.gleam b/src/content/chapter0_basics/lesson16_blocks/code.gleam new file mode 100644 index 0000000..31e4729 --- /dev/null +++ b/src/content/chapter0_basics/lesson16_blocks/code.gleam @@ -0,0 +1,13 @@ +import gleam/io + +pub fn main() { + let fahrenheit = { + let degrees = 64 + degrees + } + // io.debug(degrees) // <- This will not compile + + // Changing order of evaluation + let celsius = { fahrenheit - 32 } * 5 / 9 + io.debug(celsius) +} diff --git a/src/content/chapter0_basics/lesson16_blocks/en.html b/src/content/chapter0_basics/lesson16_blocks/en.html new file mode 100644 index 0000000..bc82e39 --- /dev/null +++ b/src/content/chapter0_basics/lesson16_blocks/en.html @@ -0,0 +1,23 @@ +

+ Blocks are one or more expressions grouped together with curly braces. Each + expression is evaluated in order and the value of the last expression is + returned. +

+

+ Any variables assigned within the block can only be used within the block. +

+

+ Try uncommenting io.debug(degrees) to see the compile error from + trying to use a variable that is not in scope. +

+

+ Blocks can also be used to change the order of evaluation of binary operators + expressions. +

+

+ * binds more tightly than + so the expression + 1 + 2 * 3 evaluates to 7. If the 1 + 2 should be + evaluated first to make the expression evaluate to 9 then the expression can be + wrapped in a block: { 1 + 2 } * 3. This is similar to grouping + with parentheses in some other languages. +

diff --git a/src/content/chapter0_basics/lesson16_lists/code.gleam b/src/content/chapter0_basics/lesson16_lists/code.gleam deleted file mode 100644 index 646ad6e..0000000 --- a/src/content/chapter0_basics/lesson16_lists/code.gleam +++ /dev/null @@ -1,16 +0,0 @@ -import gleam/io - -pub fn main() { - let ints = [1, 2, 3] - - io.debug(ints) - - // Immutably prepend - io.debug([-1, 0, ..ints]) - - // Uncomment this to see the error - // io.debug(["zero", ..ints]) - - // The original lists are unchanged - io.debug(ints) -} diff --git a/src/content/chapter0_basics/lesson16_lists/en.html b/src/content/chapter0_basics/lesson16_lists/en.html deleted file mode 100644 index c29758a..0000000 --- a/src/content/chapter0_basics/lesson16_lists/en.html +++ /dev/null @@ -1,19 +0,0 @@ -

- Lists are ordered collections of values. -

-

- List is a generic type, having a type parameter - for the type of values it contains. A list of ints has the type - List(Int), and a list of strings has the type - List(String). -

-

- Lists are immutable single-linked lists, meaning they are very efficient to - add and remove elements from the front of the list. -

-

- Counting the length of a list or getting elements from other positions in the - list is expensive and rarely done. It is rare to write algorithms that index - into sequences in Gleam, but when they are written a list is not the right - choice of data structure. -

diff --git a/src/content/chapter0_basics/lesson17_constants/code.gleam b/src/content/chapter0_basics/lesson17_constants/code.gleam deleted file mode 100644 index aed6fb0..0000000 --- a/src/content/chapter0_basics/lesson17_constants/code.gleam +++ /dev/null @@ -1,13 +0,0 @@ -import gleam/io - -const ints: List(Int) = [1, 2, 3] - -const floats = [1.0, 2.0, 3.0] - -pub fn main() { - io.debug(ints) - io.debug(ints == [1, 2, 3]) - - io.debug(floats) - io.debug(floats == [1.0, 2.0, 3.0]) -} diff --git a/src/content/chapter0_basics/lesson17_constants/en.html b/src/content/chapter0_basics/lesson17_constants/en.html deleted file mode 100644 index f610dcd..0000000 --- a/src/content/chapter0_basics/lesson17_constants/en.html +++ /dev/null @@ -1,18 +0,0 @@ -

- As well as let assignments Gleam also has constants, which are defined at the - top level of a module. -

-

- Constants must be literal values, functions cannot be used in their - definitions. -

-

- Constants may be useful for values that are used throughout your program, - permitting them to be named and to ensure there are no differences in the - definition between each use. -

-

- Using a constant may be more efficient than creating the same value in - multiple functions, though the exact performance characteristics will depend - on the runtime and whether compiling to Erlang or JavaScript. -

diff --git a/src/content/chapter0_basics/lesson17_lists/code.gleam b/src/content/chapter0_basics/lesson17_lists/code.gleam new file mode 100644 index 0000000..646ad6e --- /dev/null +++ b/src/content/chapter0_basics/lesson17_lists/code.gleam @@ -0,0 +1,16 @@ +import gleam/io + +pub fn main() { + let ints = [1, 2, 3] + + io.debug(ints) + + // Immutably prepend + io.debug([-1, 0, ..ints]) + + // Uncomment this to see the error + // io.debug(["zero", ..ints]) + + // The original lists are unchanged + io.debug(ints) +} diff --git a/src/content/chapter0_basics/lesson17_lists/en.html b/src/content/chapter0_basics/lesson17_lists/en.html new file mode 100644 index 0000000..c29758a --- /dev/null +++ b/src/content/chapter0_basics/lesson17_lists/en.html @@ -0,0 +1,19 @@ +

+ Lists are ordered collections of values. +

+

+ List is a generic type, having a type parameter + for the type of values it contains. A list of ints has the type + List(Int), and a list of strings has the type + List(String). +

+

+ Lists are immutable single-linked lists, meaning they are very efficient to + add and remove elements from the front of the list. +

+

+ Counting the length of a list or getting elements from other positions in the + list is expensive and rarely done. It is rare to write algorithms that index + into sequences in Gleam, but when they are written a list is not the right + choice of data structure. +

diff --git a/src/content/chapter0_basics/lesson18_constants/code.gleam b/src/content/chapter0_basics/lesson18_constants/code.gleam new file mode 100644 index 0000000..aed6fb0 --- /dev/null +++ b/src/content/chapter0_basics/lesson18_constants/code.gleam @@ -0,0 +1,13 @@ +import gleam/io + +const ints: List(Int) = [1, 2, 3] + +const floats = [1.0, 2.0, 3.0] + +pub fn main() { + io.debug(ints) + io.debug(ints == [1, 2, 3]) + + io.debug(floats) + io.debug(floats == [1.0, 2.0, 3.0]) +} diff --git a/src/content/chapter0_basics/lesson18_constants/en.html b/src/content/chapter0_basics/lesson18_constants/en.html new file mode 100644 index 0000000..f610dcd --- /dev/null +++ b/src/content/chapter0_basics/lesson18_constants/en.html @@ -0,0 +1,18 @@ +

+ As well as let assignments Gleam also has constants, which are defined at the + top level of a module. +

+

+ Constants must be literal values, functions cannot be used in their + definitions. +

+

+ Constants may be useful for values that are used throughout your program, + permitting them to be named and to ensure there are no differences in the + definition between each use. +

+

+ Using a constant may be more efficient than creating the same value in + multiple functions, though the exact performance characteristics will depend + on the runtime and whether compiling to Erlang or JavaScript. +

-- cgit v1.2.3