Skip to content

Modules

Julius Paffrath edited this page Dec 8, 2016 · 5 revisions

Basics

Modules in jask are single files containing jask code. You can use modules to share your code with others so they can use it. This simple example will show you how modules work: Let's say you build a super fancy function called unicorn:

function unicorn()
    printLine("Unicorns live among us!")
end

Saved it in a file called unicornModule.jask and share it! Now your friends can import it using the use statement:

; import the module
use unicornModule

; and use the funtion!
unicorn()

There are only two things to keep in mind:

  • jask searches for modules in the current directory
  • Import modules without the extension '.jask' If you have stored your modules in a different location, you can tell jask where to search:
use /my/module/path/unicornModule

That's all!

Functions with the same name

Have a look at the following module:

function helloMessage()
    printLine("I am a module!")
end

If you import this module in your jask code, you can call the function helloMessage. But what if your code also contains a function named helloMessage? Then, your local function will have a higher priority and gets called:

use moduleWithHelloMessage

function helloMessage()
    printLine("I am jask code")
end

helloMessage()

This will print

jask ~> I am jask code

Removing modules

jask gives you the ability to remove modules during code executing:

use jcore/jnumber

printLine(PI())

remove jcore/jnumber

Use this option when you no longer need the module.