LDMud logo: an emblem with three snakes, on the left behind that emblem a cogwheel. In front of both is a sword.


Home of LDMud

View the Project on GitHub ldmud/ldmud

Data Types

LPC is a dynamically typed language, but has some also some elements of static typing. That means at runtime each value has some specific type and you cannot determine from the source code what type that will be. But it is possible to specify types in the LPC source code and thus prevent some type errors on compile-time. But this is voluntary, you can always use the type mixed which means that it can take any type.

Value Types

There are two kind of types: value and reference types. Value types are copied with each assignment. When you change a variable containing a value type only that instance is modified. On the other hand for a reference type value an instance is created in memory, and all variables it gets assigned to just get a reference to that one instance. When you change that value it's modified in all variables that contain a reference to that.

LPC offers the following value types:

A signed integral number ranging from __INT_MIN__ to __INT_MAX__, on 32 bit platforms it's 32 bit wide, and on 64 bit platforms it's 64 bit wide.
    int i = 10;
A floating point number ranging from __FLOAT_MIN__ to __FLOAT_MAX__.
    float f = 10.42;
A text. LPC strings are mutable and have variable length.
    string str = "Hello!";
Symbols are identifier names. There are used for variable names in lambda closures.
    symbol s = 'var;

Reference Types

An array is a list of values. Arrays are reference types: When you modify an element inside an array all variables holding a reference to that array see that change. But as an exception every operation that changes the length of the array creates a new array instance leaving the original instance alone. So only the variable this size changing operation was carried out with references the new array. Arrays are specified with the type of the member elements followed by a star.
    int* numbers = ({1, 2, 4, 8});
Members can be accessed using the [] operator. Arrays are zero-based, so the first element has index 0.
    numbers[3] = 9;
A mapping, also known as associative array, dictionary or hash table, maps a key to one or more values. In LPC there are also mappings without a value implementing a set.
    mapping m = ([ "key1": 10, "key2": 20 ]);
mapping set = ([ "abc", "def", "xyz" ]);
Similar to arrays values are accessed using the [] operator:
    m["key3"] = 30;
Structs group a collection of values together. Each struct must be defined first:
    struct Foo
int member1, member2;
string text;
    struct Foo var = (<Foo> 10, 20, "text");
var->member2 = 42;
As LPC is an object-oriented language, objects are the main ingredient. An object variable contains the reference to one.
    object ob = this_player();

Closures reference executable code, usually other functions in an object, but may also reference efuns (driver functions) or code that was compiled at run-time (lambda functions).
    closure cl = #'write;
Closures are usually not mutable. They are executed with funcall:
    funcall(cl, "Hello, World!");

As a specialty of LPC each variable can always contain the value 0 regardles of its specified type. That is also the initial value of each variable and the one value that is considered 'false' in conditional expressions.