|
|
Log in / Subscribe / Register

Flexible metaprogramming with Rhombus

By Daroc Alden
June 30, 2026

Lisp-like languages have historically led the world in metaprogramming and flexibility. While many modern languages have adopted the idea of macros, Lisp-like languages such as Racket have continued pushing the envelope, attempting to make macros as easy as possible to incorporate into everyday programs. On the other hand, Lisp's minimal, parenthesis-based syntax can be hard to adapt to — to the point that Lisp is sometimes said to stand for "Lots of Irritating Silly Parentheses". Rhombus is a new programming language that aims to have the best of both worlds, marrying Racket's metaprogramming capabilities to a simple Python-like syntax and reasonable standard-library defaults.

The project

The language is part of the broader Racket project, which is "rooted in academia". Of Rhombus's 43 contributors, the two most active are Matthew Flatt and Wing Hei Chan, of the University of Utah and the Chinese University of Hong Kong, respectively. Rhombus is used as a teaching tool at both of their universities the University of Utah; Chan contributes to Rhombus independently, and doesn't actually work in the Chinese University of Hong Kong's computer-science department. Despite that, the language aims to be more than just an academic exercise. Development is supported by the Racket Programming Language Foundation, which seeks to make Racket and Rhombus suitable for real-world professional use. Even though the language only celebrated its 1.0 release on June 22, there is already a set of tools for the Economancy card game partially written in it, among other non-academic uses.

The core of Rhombus is available under either the MIT or Apache 2.0 license, although it is built on top of the Racket runtime, which includes LGPL 3.0 code in some dynamic libraries. That reuse of much of Racket's infrastructure means that it already has a large number of usable libraries, and an optimizing compiler. Rhombus programs can be interpreted, compiled to bytecode, or compiled to native machine code.

One place that Rhombus differs from Racket, other than its syntax, is in its choice of default data structures. Lisp-like languages often use singly linked lists as a core data type, which comes with efficiency problems. Rhombus uses lists backed by an immutable tree structure that makes many operations take logarithmic time. Flat, mutable arrays, tree-based maps and sets, and hash-based maps and sets are also available in the standard library.

The syntax

Rhombus's syntax is fairly similar to Python in that it is indentation-based, and uses ":" to indicate the start of an indented block. One slight difference is that Rhombus uses the "|" symbol to separate branches in if expressions and similar contexts. The clearest way to explain is probably with an example (an implementation of the factorial function):

    fun factorial(n):
      if n <= 1
      | 1
      | n * factorial(n - 1)

Programmers with a more imperative bent might prefer the version below that uses a for loop. Note the use of an inclusive range, 1..=n rather than the more common exclusive range 1..n.

    fun factorial(n):
      def mutable p = 1
      for (i in 1..=n):
        p := p*i
      p

By default, variables are immutable, so the mutable binding operator is needed to let the loop alter the value of p. The example can be made shorter, however. Rhombus's for loops support a feature that is uncommon outside of Lisp-like languages: an optional "reducer" that is used to combine the result of each loop into a final value for the loop itself. The math.product reducer multiplies all of the values returned from the loop, allowing one to write:

    fun factorial(n):
      for math.product (i in 1..=n):
        i

This ability means that Rhombus's equivalent of the list comprehensions found in other languages is just a for loop that uses the List reducer to collect results into a list. For loops can also produce maps, sets, or even boolean values using the all or any reducers. In a typical programming language, one might expect this kind of built-in syntax to work for the types provided by the standard library, but not for user-defined types. Rhombus's macros, however, can be used to extend more or less any part of the language, including creating new reducers. Most programs will probably not need to do so, but it is nice to have the option when it would make the program simpler.

Macros

Macros in Rhombus use single quotes to produce syntax objects that represent a fragment of Rhombus code, with "$" used to indicate placeholders. Unlike traditional Lisp-like languages that represent code with lists, Rhombus's syntax objects are their own data type. This lets them carry additional metadata such as line number and position, which in turn lets Rhombus produce helpful error messages, such as stack traces that take macros into account correctly. Syntax objects also carry metadata that controls how names are resolved, giving programmers control over when and how macros capture names from the environment. Both hygienic and non-hygienic macros are possible to write, although hygienic macros are the default.

As an example of a small macro, here is all of the code needed to implement Ruby's postfix if in terms of the built-in when macro:

    import:
      rhombus/meta open // Needed for defining a macro

    expr.macro '$action if $test':
      'when $test
       | $action'

    println("Hi!") if (foo == bar)

In some ways, this kind of syntax object is reminiscent of the approach that TemplateHaskell uses. Unlike TemplateHaskell, however, Rhombus macros can be defined anywhere that a normal function could be, and respond to the grammatical context in which they are used. The example above is a macro that can be used anywhere that an expression is expected. Reducer macros are defined with reducer.macro, and there are similar constructs for macros that customize the handling of imports, pattern matches, and so on.

One might expect that the list of grammatical contexts in which macros can operate to be fixed. That isn't true in Rhombus, which exposes functions for manipulating grammatical contexts to the user. As long as the program uses Rhombus's basic indentation-based syntax, the programmer is free to define their own grammar and kinds of expressions, should they think it necessary.

Other forms of extension

Another example of that kind of flexibility is Rhombus's approach to type checking. Rhombus is, in some sense, not a statically typed language. It can look like it is, though, if one adds type annotations:

    fun factorial(n :: Int) :: Int:
      def mutable p :: Int = 1
      for (i in 1..=n):
        p := p*i
      p

    // Calling factorial("foo") results in an error message:
    // factorial: argument does not satisfy annotation
    //  argument: "foo"
    //  annotation: Int

Rhombus allows programmers to attach arbitrary static information to expressions, and define rules for how that static information should be inferred, extended, checked, and used to generate machine code. By default, the language treats annotations as part of a gradual type system that infers types as much as possible, and then uses that to provide error messages and generate better machine code. But there is nothing stopping the programmer from attaching their own classes of static information to Rhombus programs. For example, it is possible to define a system of annotations for tracking the flow of sensitive data through a program, or for tracking the asymptotic run-time of a function. Macros can access this static information in order to compile expressions differently depending on the inferred value.

Rhombus's tendency to expose all of the implementation details of the language within the language itself is part of the project's commitment to language-oriented programming. For many tasks, users will have no need to reach for macros; the ordinary tools provided by the language are sufficient for every-day use. For those tasks where Rhombus is not already a good fit, for whatever reason, the language encourages the creation of miniature embedded domain-specific languages, to make expressing the core logic of the problem as simple as possible.

In many languages, a certain amount of boilerplate is necessary, especially for programs that are best described in terms of data structures or relationships that aren't supported as part of the standard library. Rhombus allows programmers to write that boilerplate once, in a macro, and then focus on expressing their program in whatever way makes the most sense to them, even when that requires looking deep into the semantics of loops or the action of the type system. Many such miniature language extensions can coexist within the same program, and the additional metadata attached to syntax objects allows for the creation of generic code formatters and highlighters, jump-to-definition support that handles custom binding macros, and the other conveniences expected of a modern language.

In support of its extensibility, Rhombus has extensive documentation covering all parts of the language, from parsing all of the way through to compilation. Rhombus inherits the Racket community's penchant for thorough documentation, which is a blessing when one needs to investigate the details of some part of the language, but which can be a bit verbose when one just needs a simple overview.

Rhombus's niche

Overall, Rhombus's best features are definitely its extensibility and documentation. In other areas such as compilation speed, speed of execution, package ecosystem, error messages, and so on it is solidly middle-of-the-road: good enough not to get in the way, but not so good that it outshines existing languages that focus on those areas. Rhombus code, even with full type annotations, is unlikely to challenge C or Rust in speed, for example — it typically has performance between JavaScript and Ruby, although the details vary between programs and benchmarking setups.

Where Rhombus is likely to be most useful is in those problem domains that have custom home-grown languages or that are awkward to express with existing approaches. Programmers have always had the ability to write new special-purpose programming languages, but the amount of work that goes into writing a traditional optimizing compiler is enormous. Writing a set of Rhombus macros is much easier, and gives one an existing compiler, existing libraries, usable syntax highlighting, error messages, and editor integration for free, while preserving the ability to customize the operation of the language at the deepest levels, should it prove necessary.



to post comments

tail recursion

Posted Jun 30, 2026 21:49 UTC (Tue) by ballombe (subscriber, #9523) [Link] (2 responses)

... and of course programmers that understand what they are doing will do
fun factorial(n,p):
if n <= 0
| p
| factorial(n - 1,p*n)

tail recursion

Posted Jul 1, 2026 1:06 UTC (Wed) by daroc (editor, #160859) [Link] (1 responses)

Yes, that version also works. Of course, all the versions presented here are going to be turned into an imperative loop (or constant folded) by a decent optimizing compiler. Rhombus doesn't guarantee that it optimizes the original non-tail-recursive version, but peeking at the disassembly suggests that it does in this case.

I didn't go with the tail-recursive version for the example in this article because I wanted all the examples to have the same signature, and I didn't want to introduce Rhombus's syntax for default argument values in a way that would have distracted from the point.

tail recursion

Posted Jul 2, 2026 17:55 UTC (Thu) by usaoc (subscriber, #184893) [Link]

The version that involves mutation is likely to be slower, since our compiler doesn’t optimize mutation (so that much effort can be spent elsewhere). The idiomatic way to write an imperative loop is indeed to write a tail-recursive function, but we usually do it in the function body, so users wouldn’t need to see a confusing signature:
fun factorial(n):
  recur loop(n = n, p = 1):
    if n <= 1
    | p
    | loop(n - 1, p * n)
The recur form is like named let in Scheme/Racket, which defines a local function and immediately calls it.

macro-heavy languages

Posted Jul 1, 2026 3:41 UTC (Wed) by vinipsmaker (subscriber, #126735) [Link]

I've been developing a macro-heavy programming language since last year (and just like Rhombus, I'm using Pratt parsing). I have only respect for Rhombus. What they did with the design for the match operator (not mentioned in the lwn.net article) is something that I'm still far away from being able to achieve myself. As much as I hate indentation-based languages, I have only respect for Rhombus design.

> Where Rhombus is likely to be most useful is in those problem domains that have custom home-grown languages or that are awkward to express with existing approaches.

This is middle-out design which is used in OSH/YSH: https://www.oilshell.org/blog/2022/03/middle-out.html

Like Elixir and Erlang?

Posted Jul 1, 2026 7:03 UTC (Wed) by kleptog (subscriber, #1183) [Link] (3 responses)

While reading this I was wondering if this was like how Elixer improved on Erlang. Turns out that comparison has been made, except it's even more tightly integrated. Rather than just compiling to the same VM, it macros expands to Racket directly (though I think Elixer did that in the beginning too). It's more like a partner, rather than coming later to make to original easier to use.

I do agree with their view that metaprogramming is the way forward for managing complex systems. OOP wanted to make programming easier by encapsulating objects, but that doesn't go far enough. C macros get you a long way (see the Linux kernel) but it's fundamentally limited by the underlying language. Python added more metaprogramming possibilities but it can't change the syntax (a deliberate choice).

I wonder if LLMs can finally make the use of embedded DSLs feasible, by helping clear the gap of having the learn a DSL to be able to use it effectively. DSLs have the possibility of abstracting away vast amounts of boilerplate while keeping the code readable. Do Linux device drivers really need the full power of C or could they be written at a higher level and having a compiler deal with the hardware abstractions? Can Rust deliver this? Could a DSL realise the dream of have a single codebase that could work as a built-in driver or as FUSE, just by recompiling?

A fascinating area of development.

Like Elixir and Erlang?

Posted Jul 1, 2026 8:41 UTC (Wed) by NAR (subscriber, #1313) [Link] (2 responses)

I have reservations about inventing new DSLs. After all, these are new programming languages (however limited they are), one more thing a newcomer needs to learn before being able to contribute to the project. Also, a "regular" programming language might have IDE support, syntax highlighting, language server, etc. - the fancy new project-specific DSL probably doesn't have these. My experience with Elixir was that each "major" library I used (Phoenix, Ecto, Absinthe) have their DSLs which look like Elixir, but not exactly are Elixir, leading to much confusion (at least in my case), especially when I was a newcomer to Elixir itself.

Like Elixir and Erlang?

Posted Jul 1, 2026 14:37 UTC (Wed) by daroc (editor, #160859) [Link]

One of the nice things about Rhombus is that new DSLs do have IDE support, syntax highlighting, etc. out of the box. But having reservations about making newcomers learn idiosyncratic syntax is certainly reasonable — and has been a criticism of Lisp-like languages for many years.

Like Elixir and Erlang?

Posted Jul 1, 2026 14:41 UTC (Wed) by kleptog (subscriber, #1183) [Link]

Yeah, that kind of what I mean. The idea is that a DSL is easier to understand than a general programming language and the only barrier is that it's harder to write if you're unfamiliar with it. This is the kind of tasks that LLMs excel at.

This would require LLMs to scale down far enough that you can run a useful one for this purpose locally on commodity hardware. Not sure how long that will take.

Great related work section in their paper.

Posted Jul 1, 2026 13:44 UTC (Wed) by ejr (subscriber, #51652) [Link]

Thanks! I would have ignored yet-another-macro-system without such a nice, quick summary. This article lead me to their paper (open access: https://dl.acm.org/doi/10.1145/3622818 ). I highly recommend the Related Work section. They left out Julia, but it fits in with the Lisp/Scheme style.

correction

Posted Jul 2, 2026 17:35 UTC (Thu) by usaoc (subscriber, #184893) [Link] (1 responses)

> Of Rhombus's 43 contributors, the two most active are Matthew Flatt and Wing Hei Chan, of the University of Utah and University of Hong Kong, respectively.

Just a correction: I (Wing Hei Chan) work at the Chinese University of Hong Kong (CUHK), not the University of Hong Kong (HKU). Also, I’m not affiliated with the CSE department, although I do currently work as a research staff (in linguistics, not CS). I work on Rhombus because I’m interested in programming language theory. I’m not sure if the CSE department here do use Rhombus/Racket for teaching, but my guess is not, since they seem less theory-oriented. Although I would’ve loved if we all did start teaching it :)

correction

Posted Jul 2, 2026 17:49 UTC (Thu) by daroc (editor, #160859) [Link]

Oops — that is entirely my mistake. I didn't realize that the Chinese University of Hong Kong and the University of Hong Kong were different institutions, although it makes sense in retrospect. I'll add a correction to the article. Thanks!

Examples?

Posted Jul 3, 2026 7:01 UTC (Fri) by hgouni (subscriber, #166382) [Link] (1 responses)

> For example, it is possible to define a system of annotations for tracking the flow of sensitive data through a program, or for tracking the asymptotic run-time of a function.

Are these written up somewhere? A cursory glance through the Rhombus documentation didn't turn up anything.

Examples?

Posted Jul 3, 2026 12:12 UTC (Fri) by daroc (editor, #160859) [Link]

There is not any published example code for that; I put together a series of half-baked prototypes to validate the claims. Chapter three of the Rhombus Language Model documentation has the information on how to actually create new types of static information:

https://docs.racket-lang.org/rhombus-model/static-info.ht...

In short, you need to augment binding and definition forms to add static information to expressions, it gets propagated through the program in a basically reasonable way that you can customize at function calls, and then macros can access the static information when deciding how an expression should be represented.


Copyright © 2026, Eklektix, Inc.
This article may be redistributed under the terms of the Creative Commons CC BY-SA 4.0 license
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds