Mochaccino
Share
Explore

Modules

Modules are very similar to namespaces. They provide a way to group together properties and methods related to a particular feature.
For example, the following snippet categorises different services offered by a cafe:
package cafe;

module CoffeeMachine {
var cupsLeft = 10;
var isWorking = true;
func serve()<void> {
Console.log("coffee is served!");
}
}

module IceCreamMachine {
var conesLeft = 10;
var isWorking = true;
func serve()<void> {
Console.log("coffee is served!");
}
}

module Main implements <EntryPoint> {
func main()<void> {
CoffeeMachine.serve();
IceCreamMachine.serve();
}
}
Nested Modules
Consider the following expression:
CoffeeMachine.Beans.brew();
Seems appealing doesn’t it? Well, we call this module nesting, where the Beans module is nested inside the CoffeeMachine module. This is used to further organise functionality of code within modules. To do so, declare a module like you normally would, just inside an existing module.
module CoffeeMachine {
var cupsLeft = 10;
var isWorking = true;
func serve()<void> {
Console.log("coffee is served!");
}

module Beans {
func brew()<void> {
Console.log("brewing beans!");
}
}
}
Module Polyphormism
Share
 
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.