TL;DR
Modules are way how to group Elm functions in meaningful groups. This post is part of the functional language series.
In Elm, the module is starting block. Elm is a functional programming language, which means that everything happens in functions. Elm creators decided that Elm’s function must have a module.
Let's create a string from number 46 using the Debug module that comes with Elm.
Have you helped yourself with a great TAB
feature in repl
? No, because elm repl
does not support it. But history works, you can use up and down arrows for history feature. Time to use your favorite editor and create an Elm module.
You need to initialize your first elm project:
elm init
For now, you just need to know that it creates src
the folder where you should put your elm source files. Create a file with the name src/Vat.elm
. Elm requires that the module name and file name are the same.
Congratulations, you created your first elm module. You learned the following keywords:
module
this is how you start the module definition.Vat
is the module name.
Remember that the module name should be the same as the module file name. Start module name with uppercase. The module name should give a reader a hint about what this module do.
exposing
a list of functions and variables that are available to the world.
Remember, do not use exposing on module definition but on the module
import
. This will make your code more readable.
- We declared a variable
cro
that represents the Croatian Vat tax value. elm
is Elm file extension
Let’s use Vat
in repl:
elm make src/Vat.elm
elm make
compiles Vat.elm so it could be run:
Note that cro
has elm Float
a datatype.
Remember
- how to initialize the project
- how to create a module
- module name naming convention
- how to compile a module
- how to use it in
elm repl