Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Modules page readability #693

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/syntax_and_semantics/generics.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Generics

Generics allow you to parameterize a type based on other type. Consider a Box type:
Generics allow you to parameterize a type based on another type. Generics provide type-polymorphism. Consider a Box type:

```crystal
class MyBox(T)
Expand Down Expand Up @@ -48,9 +48,9 @@ string_box = MyBox.new("hello") # : MyBox(String)

In the above code we didn't have to specify the type arguments of `MyBox`, the compiler inferred them following this process:

* The compiler generates a `MyBox.new(value : T)` method, which has no explicitly defined free variables, from `MyBox#initialize(@value : T)`
* The `T` in `MyBox.new(value : T)` isn't bound to a type yet, and `T` is a type parameter of `MyBox`, so the compiler binds it to the type of the given argument
* The compiler-generated `MyBox.new(value : T)` calls `MyBox(T)#initialize(@value : T)`, where `T` is now bound
- The compiler generates a `MyBox.new(value : T)` method, which has no explicitly defined free variables, from `MyBox#initialize(@value : T)`
- The `T` in `MyBox.new(value : T)` isn't bound to a type yet, and `T` is a type parameter of `MyBox`, so the compiler binds it to the type of the given argument
- The compiler-generated `MyBox.new(value : T)` calls `MyBox(T)#initialize(@value : T)`, where `T` is now bound

In this way generic types are less tedious to work with. Note that the `#initialize` method itself does not need to specify any free variables for this to work.

Expand Down
6 changes: 4 additions & 2 deletions docs/syntax_and_semantics/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

Modules serve two purposes:

* as namespaces for defining other types, methods and constants
* as partial types that can be mixed in other types
- as namespaces for defining other types, methods and constants
- as partial types that can be mixed in other types

An example of a module as a namespace:

Expand All @@ -18,6 +18,8 @@ Curses::Window.new

Library authors are advised to put their definitions inside a module to avoid name clashes. The standard library usually doesn't have a namespace as its types and methods are very common, to avoid writing long names.

## `extend` and `include`

To use a module as a partial type you use `include` or `extend`.

An `include` makes a type include methods defined in that module as instance methods:
Expand Down