Loci Programming Language

See Loci Website.

Loci is a multi-paradigm systems programming language. Or, to describe it in a slightly more intuitive way, it’s very similar to, and a close competitor of, C++.

It’s a language I’ve been designing since around mid-2009, that aims to not only fix many of the problems that plague C++, but also to introduce whole new paradigms and programming styles that are extremely useful for developers. Alongside that, of course, the language aims to have no performance overhead versus C and C++.

Before I go further, here’s some example code:

The output is:

So hopefully the first thing that strikes you is that the language shares the look and feel of C++. On the other hand you’ll also notice differences that are designed to enhance the development experience, such as structural typing, which allows a class instance to be casted to an interface instance as long as the class provides all the methods required by the interface.

This means the end of explicit declarations of polymorphic inheritance, which can be particularly valuable if you’re hoping to create an interface after the classes that implement it have already been written (or, e.g. are in the standard library). Note there’s almost always (unless the interface has a large number of methods) no performance penalty for this feature.


But this is really just a drop in the ocean and it would take many pages to describe the language in detail, as you can see if you look at the language documentation. So here’s a brief summary of the key features Loci offers (in addition to the above):

  • Multi-pass compilation – C++ developers regularly struggle with the need for symbol declarations to appear before their use (except for the few cases where this isn’t true); this just isn’t a problem in Loci, which can match up declarations and uses regardless of their relative ordering.
  • Modules – groups of files can be combined as ‘modules’, on the order of which it’s possible to express exported and imported symbols (via the ‘export’ and ‘import’ keywords). It’s also a great way to enable more inter-procedural optimisations across source files.
  • No more header files – by a combination of modules (to handle intra-module dependencies), and auto-generated headers (for inter-module dependencies), Loci removes the need for developers to write header files, and therefore also avoids the need to endure the pain of header guards.
  • Templates – just like C++, you can automatically generate functions and types by creating templates. Unlike C++, you can express your type requirements via interfaces. Furthermore, ‘virtual templates’ provide a key mechanism for polymorphism.
  • Primitives are objects – other languages tend to encourage the notion that primitives are separate from user defined types, but Loci breaks down this barrier. This means you can call methods on primitive types (e.g. (-1).abs();), and cast primitives to interfaces in the same way as user defined types. Yet again there’s no performance penalty for this feature.
  • Move by default, copy by choice – C++ expects every type to have a copy constructor, generating one if none is specified. Loci is completely different, allowing moves on any type by default, but only allowing a type to be copied if it has a copy method.
  • Algebraic Datatypes – with Loci, it’s possible to easily build data values, and to then pattern-match those values. This therefore provides an efficient and type-safe alternative to typical combinations of enums, structs and unions.

The last point there sounds a little cryptic, but in fact algebraic datatypes are a really simple and powerful concept, and the basis of functional programming. Here’s an example:

As you probably expect, this prints:

This is a really basic example, but it shows the elegance of this solution in comparison to using combinations of enums, structs and unions. In fact, this solution has the further technical advantage that it can call type destructors (since the compiler is aware of the internal enum), and you can therefore put arbitrarily complex types in your datatypes.


Locic is the name of the Loci compiler, which is in active development here (GitHub repository). The compiler is written in C++, and uses LLVM for the back-end.

The compiler tools repository (and 1.1 release) contains a large set of documentation (which is generated in HTML, MAN and PDF forms by Sphinx), as well as numerous examples using the language which are built by the compiler. The documentation also forms the Loci website.

One Response to Loci Programming Language