A tour of the language¶
This page shows the shape of Mamba. Each part links to the reference for details. If you have written C#, Java or TypeScript, most of it will look familiar.
Operations¶
Code is written in operations: named blocks with a return type and parameters. An operation is a method of a class, an action of a form, or an operation of a service.
function decimal Total(decimal taxRate)
{
return this.Price + this.Price * taxRate;
}
static function Domain.Product CreateDraft(string name)
{
var product = Domain.Product.Create();
product.Name = name;
return product;
}
functiondeclares an operation. The return type comes after it:bool,decimal,void,Domain.Product.staticmeans it belongs to the class, not to one object. Withoutstatic,thisis the object the operation was called on.- Statements end with a semicolon, and blocks use braces.
Variables¶
int count = 0;
string name = "Ada";
var product = Domain.Product.Create(); // the type is taken from the right side
Collection[Domain.Product] cheap; // an empty collection, ready to use
A variable has a type, and the type is checked. var lets the engine work the type out. See Types and variables.
Comments¶
Conditions¶
if stock == 0
{
status = "Sold out";
}
elseif stock < 5
{
status = "Low";
}
else
{
status = "Available";
}
Parentheses around the condition are optional. Note that elseif is one word. There is also switch, and the conditional operator ?:. See Conditions.
Loops¶
foreach product in products
{
total = total + product.Price;
}
for (int i = 0; i < 10; i++) { }
while count < 100 { count = count + 1; }
See Iterations.
Lambdas¶
A lambda is a small function written as x => expression. It is how you filter, sort and transform collections, and how you tell a query what to find.
var expensive = Domain.Product.Find(p => p.Price > 100);
bool anyFree = products.Any(p => p.Price == 0);
Errors¶
try
{
order.Submit();
}
catch Exception e
{
DebugLib.Logger.WriteErrorLine("Submit failed: " + e.Message);
}
throw BusinessException.Create("The order has no lines.");
Throw a BusinessException for a problem the user should hear about, such as a broken rule. See Exceptions.
Calling libraries¶
The built-in libraries are available everywhere, by name:
DebugLib.Logger.WriteInfoLine("Started");
var user = AppLib.Session.GetCurrentUser();
string text = string.Format("Hello {0}", name);
See Libraries.
Strings and text¶
Strings are joined with +. A value of any type is turned into text when added to a string, and value + "" is the short way to convert. string.Format("{0} of {1}", a, b) fills in placeholders. Text that the user sees should come from localized resources, written LocalResources.Key, so that it can be translated.
Putting it together¶
A form action that finds a product, changes it, and saves it. First() returns null when nothing matches, so the action checks for that: