Types and variables¶
Mamba is statically typed. Every variable, parameter and return value has a type, and the type is checked when you validate the model.
Built-in types¶
These come from the Standard Library, where each type has its own methods.
| Type | Holds |
|---|---|
bool |
true or false. |
byte, int, long |
Whole numbers of increasing size. |
float, double, decimal |
Fractional numbers. Use decimal for money. |
char |
One character. |
string |
Text. |
DateTime |
A date and time. See Time. |
TimeSpan |
A length of time. |
Guid |
A globally unique identifier. |
Object |
Any value. Use it only when you must. |
void |
No value. The return type of an operation that returns nothing. |
Business object types¶
Every class and enumeration in your business objects is a type, written with the Domain. prefix.
Domain.Product product = Domain.Product.Create();
Domain.OrderStatus status = Domain.OrderStatus.Open;
Their attributes, associations and operations are available with a dot. If a class has an association to Category, product.Category.Name follows it. See Class and Enumeration.
The same types are also available from the Domain library, which lists the operations every class gets automatically.
Structures of an API adapter are types too, written Interfaces.<Adapter>.<Structure>.
Collections¶
| Type | Use it for |
|---|---|
Collection[T] |
An ordered list that grows and shrinks. Query results are collections. |
Dictionary[K, V] |
Values found by a key. |
Array[T] |
A fixed-size list. |
Collection[string] names;
names.Add("Ada");
names.Add("Grace");
Dictionary[string, int] stock;
stock.Add("pen", 12);
var count = names.Length; // the size is Length, not Count
A collection that you declare starts empty and is ready to use. An Array starts as null, so create it before you read from it.
You can write an empty collection as {}.
What you can do with a collection¶
Add, AddRange, Remove, Clear, Contains, IndexOf, Any, All, Where, Select, SelectMany, First, Last, Sum, Max, Min, OrderBy, OrderByDescending, ThenBy, Distinct, Take, Skip and ToCollection. The full list with examples is in the Standard Library.
Declaring without a value¶
A variable of a business object type that you declare without a value is not null. It is a new, empty object:
So product == null is false here. To find out whether a record exists, use Find(...).First(), which returns null when there is no match. See Behaviour to know.
Conversions¶
Convert a value to text with ToString() or by adding an empty string, and parse text with the Parse method of the target type.
string text = count.ToString();
int number = int.Parse("147");
decimal price = decimal.Parse("12.50", "en-us"); // the culture is a string
double d = 3.7;
float f = d as float; // a cast
A number converts to a wider type without a cast. Anything that does not convert implicitly needs as or an explicit conversion, and Validation reports it if you forget.
Generic types¶
Types in square brackets, such as Collection[Domain.Product], name the type of the elements. A lambda such as p => p.Price > 100 has the type Func, or Predicate when it returns true or false. You rarely write these types yourself, because the engine works them out.