Modules and Extensions

Ziv's modular system is designed to promote code reusability and maintainability. This section covers how to create, import, and extend modules in Ziv.

Table of Contents

Modules

Ziv's modular system is designed to promote code reusability and maintainability. This section covers how to create, import, and extend modules in Ziv.

Creating a Module

A module is a collection of functions, types, and variables that can be imported and used in other files. To create a module, you need to define a file with the .ziv extension and declare the module using the module keyword.

Here is an example of a simple module for mathematical operations:

# math.ziv

module math

fn add(a: int, b: int) -> int:
    return a + b

fn subtract(a: int, b: int) -> int:
    return a - b

end module

In this example, we define a module called math that contains two functions: add and subtract. These functions can be imported and used in other files.

Exporting and Importing Modules

By default, all functions, types, and variables defined in a module are public and can be accessed from other files. To import a module, you can use the import keyword followed by the module name.

If you want to restrict access to certain functions, types, or variables, you can use the private keyword to mark them as private.

private fn multiply(a: int, b: int) -> int:
    return a * b

Here is an example of importing the math module and using its functions:

# main.ziv

import math

fn main() -> void:
    result: int = math.add(1, 2)
    print(result)  # Output: 3

In this example, we import the math module and use its add function to add two numbers.

Importing Specific Functions

If you only need to import specific functions, types, or variables from a module, you can use the from keyword followed by the module name and the functions you want to import.

from math import add, subtract

Or you can import a specific function and rename it using the as keyword:

import math.add as sum

Creating Libraries

You can organize related modules into a library by creating a directory with the library name and placing the module files inside it. To import a module from a library, you can use the library name followed by the module name.

lib/
  math/
    math.ziv
  string/
    string.ziv
# string/string.ziv

module lib.string

fn capitalize(s: string) -> string:
    return s.capitalize()

end module
# main.ziv

import lib.string

fn main():
    result: string = lib.string.capitalize("hello")
    print(result)  # Output: Hello

In this example, we create a library with two modules: math and string. We import the string module from the library and use its capitalize function to capitalize a string.

Extensions

Extensions allow you to add new functionality to existing types without modifying their source code. This is useful when you want to extend the behavior of a type defined in a library or module.

Creating an Extension

To create an extension, you need to define a file with the .ziv extension and declare the extension using the extension keyword followed by the type you want to extend.

Here is an example of extending the string type with a new method:

# string_extension.ziv

extension string

fn uppercase() -> string:
    return self.upper()

end extension

In this example, we define an extension for the string type that adds a capitalize method to capitalize the string.

Importing Extensions

To use an extension, you need to import it in the file where you want to extend the type. You can import an extension using the import keyword followed by the extension file name.

# main.ziv

import string_extension

fn main():
    s: string = "hello"
    result: string = s.uppercase()
    print(result)  # Output: HELLO

In this example, we import the string_extension extension and use the uppercase method to convert a string to uppercase.

Extension Methods

Extension methods can access the properties and methods of the extended type using the self keyword. You can define extension methods for any type, including built-in types, user-defined types, and library types.


extension int

fn is_even() -> bool:
    return self % 2 == 0

end extension

In this example, we define an extension for the int type that adds an is_even method to check if the integer is even.

import int_extension

fn main():
    n: int = 10
    result: bool = n.is_even()
    print(result)  # Output: true