-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Docs updates und restructuring #14636
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9404b54
Update pattern matching docs
LostKobrakai ea2c063
Update keyword list docs
LostKobrakai 431e007
Add some more cross references
LostKobrakai 5666e6c
Correct claim around module name needing to start with uppercase
LostKobrakai 8be7348
Split out compilation and scripting docs
LostKobrakai b821d35
Move aliases and nesting to module guide
LostKobrakai d83564a
Reorder
LostKobrakai fcaa986
Add change in phrasing
LostKobrakai 06300de
Add section for elixir expressions
LostKobrakai 2bfb5c9
Revert "Split out compilation and scripting docs"
LostKobrakai b26b215
Update lib/elixir/pages/getting-started/keywords-and-maps.md
LostKobrakai 2d5c56d
Update lib/elixir/pages/getting-started/case-cond-and-if.md
LostKobrakai 45707d4
Update lib/elixir/pages/getting-started/case-cond-and-if.md
LostKobrakai 2922ca8
Move macro and hook to cond back
LostKobrakai c2c10a8
Drop expression headline level
LostKobrakai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
lib/elixir/pages/getting-started/compilation-and-scripting.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<!-- | ||
SPDX-License-Identifier: Apache-2.0 | ||
SPDX-FileCopyrightText: 2021 The Elixir Team | ||
--> | ||
|
||
# Compilation and Scripting | ||
|
||
As our examples get longer in size, it can be tricky to type them all in the shell. It's about time for us to learn how to compile Elixir code and also how to run Elixir scripts. | ||
|
||
## Compilation | ||
|
||
Most of the time it is convenient to write [modules](modules-and-functions.md) (more closely looked at in the next chapter) into files so they can be compiled and reused. Let's assume we have a file named `math.ex` with the following contents: | ||
|
||
```elixir | ||
defmodule Math do | ||
def sum(a, b) do | ||
a + b | ||
end | ||
end | ||
``` | ||
|
||
This file can be compiled using `elixirc`: | ||
|
||
```console | ||
$ elixirc math.ex | ||
``` | ||
|
||
This will generate a file named `Elixir.Math.beam` containing the bytecode for the defined module. If we start `iex` again, our module definition will be available (provided that `iex` is started in the same directory the bytecode file is in): | ||
|
||
```elixir | ||
iex> Math.sum(1, 2) | ||
3 | ||
``` | ||
|
||
## Scripting mode | ||
|
||
In addition to the Elixir file extension `.ex`, Elixir also supports `.exs` files for scripting. Elixir treats both files exactly the same way, the only difference is in intention. `.ex` files are meant to be compiled while `.exs` files are used for scripting. This convention is followed by projects like `mix`. | ||
|
||
For instance, we can create a file called `math.exs`: | ||
|
||
```elixir | ||
defmodule Math do | ||
def sum(a, b) do | ||
a + b | ||
end | ||
end | ||
|
||
IO.puts Math.sum(1, 2) | ||
``` | ||
|
||
And execute it as: | ||
|
||
```console | ||
$ elixir math.exs | ||
``` | ||
|
||
Because we used `elixir` instead of `elixirc`, the module was compiled and loaded into memory, but no `.beam` file was written to disk. | ||
|
||
Elixir projects are usually organized into three directories: | ||
|
||
- `_build` - contains compilation artifacts | ||
- `lib` - contains Elixir code (usually `.ex` files) | ||
- `test` - contains tests (usually `.exs` files) | ||
|
||
When working on actual projects, the build tool called `mix` will be responsible for compiling and setting up the proper paths for you. For learning and convenience purposes, we recommend you to write the following code into script files and execute them as shown above. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.