Primitives are the built-in object types that all other objects are composed from. They can be created through literals, atomic expressions that evaluate to a value.
All primitive values in Magpie are immutable. That means that once created, they cannot be changed.
3 is always
"hi" is always
A boolean value represents truth or falsehood. There are two boolean literals,
false. Its class is
Magpie has two numeric types, integers and floating-point. Number literals look like you expect:
// Ints 0 1234 -5678 // Floats 3.14159 1.0 -12.34
Integers and floating-point numbers are instances of different classes (
Float respectively) and there are no implicit conversions in Magpie. This means that if you have a method that expects a
Float, then passing
1 won't work. You'll need to pass
In practice, this is rarely an issue. Most arithmetic operations have specializations for both kinds of numbers and will work fine regardless of what you pass. In cases where mixed argument types are passed, then the result will be a float.
If you want to write code that works generically with either kind of number, then you want the
Num class. Both
Float inherit from that, so a method specialized to
Num will accept either type.
String literals are surrounded in double quotes:
A couple of escape characters are supported:
"\n" // Newline. "\"" // A double quote character. "\\" // A backslash.
Their class is
Characters are instance of the
Char class and represent the individual Unicode code points that make up strings. When you index into a string or iterate over it, character objects will be returned. They also have a literal form, which is the character surrounded by single quotes:
There will also be escape sequences for characters, but they haven't been implemented yet.
Magpie has a special primitive value
nothing, which is the only value of the
Nothing. (Note the difference in case.) It functions a bit like
in some languages: it indicates the absence of a value. An
if expression with
else block whose condition is
false evaluates to
nothing. Likewise, a
print() that doesn't return anything actually returns
It's also similar to
null in some ways, but it doesn't have the
null has in most other languages. It's rare that you'll
actually need to write
nothing in code since it can usually be inferred from
context but it's there if you need it.
nothing is in its own class, that means that a method that is specialized to another class won't receive
nothing instead. In Magpie, you never need to do this:
def length(string is String) // Make sure we got a string. if string == nothing then return 0 string count end
If your method expects a string, you'll get a string, not a string-or-maybe-nothing.