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

LDMud

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:

int
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;
float
A floating point number ranging from __FLOAT_MIN__ to __FLOAT_MAX__.
    float f = 10.42;
string
A text (a sequence of characters). LPC strings are mutable and have variable length.
    string str = "Hello!";
bytes
Sequences of bytes (8 bit values). Similar to strings LPC bytes are mutable and have variable length.
    bytes buf = b"Hello!";
symbol
Symbols are identifier names. There are used for variable names in lambda closures.
    symbol s = 'var;
lpctype
Variables of type lpctype can store an LPC type, that can be used to check against other values or types.
    lpctype t = [int|string];

Reference Types

Arrays
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;
mapping
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;
struct
Structs group a collection of values together. Each struct must be defined first:
    struct Foo
    {
        int member1, member2;
        string text;
    };
Usage:
    struct Foo var = (<Foo> 10, 20, "text");
    var->member2 = 42;
object
As LPC is an object-oriented language, objects are the main ingredient. An object variable contains the reference to one.
    object ob = this_player();

    ob->query_name();
lwobject
A lightweight object is a middle ground between structs and full-blown objects. They are very similar to objects but have automatic lifetime.
    lwobject lwo = new_lwobject("/obj/foo");

    lwo->set_bar(42);
closure
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!");
coroutine
A coroutine variable holds the execution state of an asynchronous function. Such a function can be suspended and continued at a later time.
    async void fun()
    {
        // Do something.

        yield(); // Pause

        // Do something later.
    }

    coroutine cr = fun();

    call_coroutine(cr);

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.