Search code examples
opa

Opa: What is the difference between modules and packages?


I read Opa documentation, but I did not catch the difference between modules and packages, and how modules or packages are linked to the source layout of an Opa application.

Can someone provide sensible examples on how and when to use modules vs. packages?


Solution

  • Packages

    You can isolate some parts of your application into different packages. For example, you can create 3 packages: model, view and controller. Just define for examples package myApp.model at the beginning of your source files that belong to the model package.

    Then, if one of your view file needs the model, just add import myApp.model at the beginning of the file.

    Instead of writing package and import in your source file, you can also use a single conf file:

    myApp.controller:
        import myApp.view
        src/controller.opa
    
    myApp.view:
        import myApp.model
        src/view.opa
    
    myApp.model:
        src/model.opa
    

    And then use the --conf compilation option.

    Modules

    A module is define this way

    module MyModule {
       function f(){ void }
    }
    

    You access to module functions with MyModule.f()

    Modules inside a Package

    So if you have a package model with a module MyModule defined in it, and you want to access to it from outside, you'll write:

    import myApp.model
    MyModule.f()
    

    Update: read messages below, they provide other useful information.