Skip to main content

Modules

·359 words·2 mins
Documentation - This article is part of a series.
Part 12: This Article

Modules Within A File
#

Modules are a way to organize code within a file. They allow you to group related code together and make it easier to manage. Below is an example of a module declared within a file.

mod math {
    fun gcd(a: Int, b: Int): Int {
        if b == 0 {
            return a;
        }
        return gcd(b, a % b);
    }
}

println(math.gcd(12, 18)); // Output: 6
Output:
6

In the example above, we have a module called math that contains a function gcd which calculates the greatest common divisor of two numbers. We can access the gcd function by using the module name followed by a dot and the function name.

Modules Across Files
#

Modules can also be imported from other files. You can do this with the mod keyword followed by the filename.

Filename: math.sg
fun gcd(a: Int, b: Int): Int {
    if b == 0 {
        return a;
    }
    return gcd(b, a % b);
}
Filename: main.sg
mod math;

println(math.gcd(12, 18)); // Output: 6

sage main.sg
Output:
6

Importing Items From Modules
#

You can import items from modules using the from and import keywords. Below is an example of importing the gcd function from the math module.

mod math {
    fun gcd(a: Int, b: Int): Int {
        if b == 0 {
            return a;
        }
        return gcd(b, a % b);
    }
}

from math import gcd;
println(gcd(12, 18)); // Output: 6
Output:
6

You may also import all items from a module using the * wildcard.

mod math {
    fun gcd(a: Int, b: Int): Int {
        if b == 0 {
            return a;
        }
        return gcd(b, a % b);
    }
}

from math import *;
println(gcd(12, 18)); // Output: 6

Output:
6

Renaming Imported Items
#

You can rename imported items using the as keyword. Below is an example of importing the gcd function from the math module and renaming it to greatest_common_divisor.

mod math {
    fun gcd(a: Int, b: Int): Int {
        if b == 0 {
            return a;
        }
        return gcd(b, a % b);
    }
}

from math import gcd as greatest_common_divisor;
println(greatest_common_divisor(12, 18)); // Output: 6
Output:
6
Documentation - This article is part of a series.
Part 12: This Article