In Mochaccino, the rules are simple. Everything is built towards code organisation and easy debugging. Let’s go through some unique features of the Mochaccino language.
Modules are the building blocks of your program. A module contains properties and methods needed for a certain functionality. For example, DBTools can be a module. It can include fields to store the HTTP Client, endpoint configurations as well as methods to make calls, process data.
module DB {
final var client<HttpClient> = HttpClient.create();
func getObjectById(id<string>)<promise<map>> async {
return await dbutils.fetch(id: id);
You can bundle multiple modules under a package using the package keyword followed by an identifier. Every Mochaccino program must have a package main that provides an entry point to your program.
Inside the package main, you must define a module that implements the EntryPoint struct. This built-in struct requires that you define a function named main that takes no arguments and returns void.
The dock keyword allows you to import packages. Mochaccino doesn’t have a package registry, so you can only import built-in packages or other local Mochaccino files. The dock statement supports selective imports, wildcard imports, and import aliases.
You can split up a package across multiple files. The inlcude keyword allows you to include the contents of another file inside the current package. Each of the included files must have a partof keyword followed by the package that is inclduing that file.
package A;
include B;
include C;
package B;
partof A;
package C;
partof A;
One caveat of using include is that included packages cannot have their own dock statements. All imports must be done by the package using the include keyword.
Static Typing
Type Annotations
Like any statically-typed language, Mochaccino uses type annotations to give variables and values types. The default type inferred is <dynamic>. You can implicitly define a variable’s type too:
var a<string> = "Hello";
Similarly, whenever a type is expected, it should be enclosed in angled brackets. You can also provide type arguments where appropriate:
var b<map<string, int>> = {'a':1};
Built-In Types
The default types in Mochaccino are:
map<K, V>
Actually, there’s another default type in Mochaccino, <EntryPoint>, which is used by the module containing the main function. However, this type is considered an exception, and the term “default types” excludes this type.
The default types are written in lowercase, while custom types are written in CamelCase.
Custom Types with Structs
To define a custom type, you use the struct keyword. This allows you to define an interface that can be used to constrain data with certain properties and methods.
Constructors and Type Assertions
A struct can have a constructor, which is empty by default, that converts data to its own type. The constructor is called whenever a type conversion (aka assertion) is taking place. A type conversion looks like this:
On the left is the value that needs to be converted and on the right is the conversion target type. In this case, the constructor for <string> is called with 12 as the argument.
Guarded Keyword
The guarded keyword in Mochaccino causes the succeeding statement to only be executed if it does not throw an error. If an except clause is specified, then the offending statement will be run, and the error will be handled by the except clause. This is typically used in type assertions, where one type is converted to another.
guarded 12::<Service> except pause; // program exits without errors
guarded 12::<Service>; // no type conversion takes place
Type Arguments
Implementing Structs
Extending Structs
Breakpoint Keywords
There are two breakpoint keywords in Mochaccino, ok and notok, which are used for debugging. The ok keyword logs its line number and column position when reached. The notok keyword prints its position, the result of the preceding statement to the console, and exits the program. Furthermore, their functionality can be extended through the use of debug flags, discussed later.
if (someVar.someMethod()!='abc') {
} else {
This is the console log for the above code:
exit[0]: notok
line: 3, column: 3
Debug Flags
Taking the same example as just now, this is how we can use debug flags for richer console logs:
if (someVar.someMethod()!='abc') {
#label: abc is null
} else {
The above code will result in a log like this:
exit[0]: abc is null
line: 3, column: 3
Rich Error Messages
Aside from pointing out the location and type of errors, Mochaccino can also suggest personalised troubleshooting tips. For exampe:
exit[1]: 1 errors
PackageResolution: package 'B' not found.
3| include B;
- ensuring the package has the "package B;" and "partof A" directives at the top.
- ensuring the package is within the project scope, by running "moc scope add "path/to/file.moc" from the same directory as your mochaccino.configs.json file.
Built-In onChange Notifier
Variables in Mochaccino come with a special onChange function that can be configured with a callback whenever said variable is changed.
var a<string> = "hello";
a.onChanged((newValue<dynamic>) {
if (newValue==null) {
throw "newValue for 'a' is null";
Used with other debugging tools, you’ll no longer have to spend hours pulling your hair out wondering where that null value is coming from.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.