gitmyhub

Cello

C ★ 7.1k updated 1y ago

Higher level programming in C

Cello is a C library that adds generics, garbage collection, exceptions, and object-like method dispatch to plain C, letting you write higher-level code without switching languages.

Csetup: moderatecomplexity 3/5

Cello is a library for the C programming language that adds features normally found in higher-level languages. C is fast and widely used, but it lacks built-in support for things like generic containers that hold any type of data, automatic memory cleanup, error handling via exceptions, and object-like behavior where the same function name works differently depending on the type it receives. Cello adds all of these on top of standard C.

With Cello you can create arrays and tables that hold typed values, loop over collections with a foreach keyword, and define custom types that can be compared, printed, and hashed without writing that logic from scratch each time. Optional garbage collection means you can allocate objects and let Cello clean them up automatically when they go out of scope, rather than tracking every free() call manually.

The library works by attaching type information to pointers at runtime, a technique the author describes as a fat pointer approach. This lets functions inspect what type of object they received and dispatch behavior accordingly, similar to how object-oriented languages handle method calls. Existing C structs can be registered with Cello to gain these abilities without rewriting them.

The author is clear that Cello started as an experiment to see how far C could be pushed, and recommends trying it on a personal project before anything production-facing. The README notes that the library has quirks and that teams working under deadlines would find better tooling and community support in C++ instead.

The source code and documentation are available, and contributions are welcome. The author also links to a book they wrote on building a Lisp interpreter in C.

Where it fits