Remember when I juggled some traits to avoid generics, and finished the post with “and I think that is beautiful”?
Well. It is still beautiful. But there were limitations to that design too.
The Problem (Again)
Rust doesn’t have inheritance. And I like that decision.
We are porting token standards (ERC20, ERC721 and friends) to Stellar Soroban. These standards come with extensions. Based on the extensions you have, you need to change the behavior of the base token. For example:
Enumerablekeeps an index of which tokens each account owns. So when a token is transferred,transferhas to do some extra bookkeeping.Consecutivemints tokens in batches, and stores ownership per range, not per token. So it doesn’t just decoratetransfer, it replaces how ownership works entirely.AllowListwantstransferto reject anyone who is not on the list.Voteswants every transfer to also move voting power around.
On top of that, some extensions cannot live together. Put Enumerable and Consecutive in the same contract, and you get garbage. So we want:
- overrides: the base behavior changes depending on which extensions the contract uses.
- mutual exclusion: incompatible extensions should not compile together. Not “panic at runtime”, not “a warning in the docs that nobody reads”. Should not compile in the first place.
- developer friendly API: should be intuitive for the end developer to understand what’s going on.
Keep these three in mind, we’ll be scoring every solution against them.
What We Did Last Time
Quick recap of Juggling with Traits for those who didn’t read it (shame on you), and for those who read it and forgot (understandable).
Rust has no negative trait bounds, so we couldn’t say “Enumerable but NOT Consecutive”. Instead, we flipped it into a positive bound. The base trait got an associated type, a single slot that says what kind of contract this is:
pub trait NonFungibleToken {
type ContractType: ContractOverrides;
fn transfer(e: &Env, from: Address, to: Address, token_id: u32) {
Self::ContractType::transfer(e, &from, &to, token_id);
}
/* other methods */
}
The default methods don’t hold the logic themselves, they forward to Self::ContractType. And each contract type (Base, Enumerable, Consecutive) is a unit struct implementing ContractOverrides, overriding only what it cares about.
Then each extension pinned that slot to its own type:
pub trait NonFungibleEnumerable: NonFungibleToken<ContractType = Enumerable> {}
pub trait NonFungibleConsecutive: NonFungibleToken<ContractType = Consecutive> {}
A contract can give ContractType only one value. Enumerable is not Consecutive. So implementing both extensions is a compile error. Mutual exclusion achieved, overrides achieved, and the end developer only writes one line:
impl NonFungibleToken for MyContract {
type ContractType = Enumerable;
}
Three out of three. Chef’s kiss.

The Small Problem
Look at that bound again:
pub trait NonFungibleEnumerable: NonFungibleToken<ContractType = Enumerable> {}
It doesn’t say “anything except Consecutive”. It says “exactly Enumerable”.
Our astute readers can foresee the problem. What if:
- we need another extension that overrides behavior
- so it needs its own type
- and can be combined with
Enumerable? - how to combine them???
Our syntax only allows for 1 concrete contract type. And we definitely don’t want to increase the slots by adding generics and making it spaghetti code.
Let’s give a more concrete example by moving to the fungible side. Some of our extensions are: AllowList, BlockList, TotalSupply, Capped, FungibleVotes… Each of them overrides something, so each of them got its own contract type, and each extension pinned the slot to its own type:
pub trait FungibleAllowList: FungibleToken<ContractType = AllowList> {}
pub trait FungibleBlockList: FungibleToken<ContractType = BlockList> {}
Now, is a token with both an allowlist and a blocklist a reasonable thing to want? Sure, why not.
It’s not possible though with this design… Let’s try:
impl FungibleToken for MyToken {
type ContractType = AllowList; // or BlockList? pick one...
}
impl FungibleAllowList for MyToken {}
// does not compile: ContractType is `AllowList`, not `BlockList`
impl FungibleBlockList for MyToken {}
Nope. Same story for an allowlisted token with voting power: votes needs the slot to be FungibleVotes, the allowlist needs it to be AllowList. One slot, two tenants.
In other words: say A and B are incompatible, and C and D are incompatible. We wanted to forbid A + B and C + D. What we actually forbade is every pair. A + C? Forbidden. B + D? Forbidden. AllowList + Votes? Forbidden.
We built mutual exclusion of everything. Every extension is incompatible with every other extension that overrides anything, purely because we have a single slot for the ContractType.
This is actually a quite fine and safe design if you don’t have many extensions (ContractTypes) that needs to be combined. Think about it, 2 extensions overrides the same behavior. It doesn’t sound safe. And this design doesn’t allow it, great!
Back then, it was enough for our needs. But after we started to implement more and more extensions that can be combined with each other, we realized how the above design became too limiting.
So, we are back at figuring out a new solution for this specific problem.

Let’s Fix It… But How?
We spent a good amount of time on this. Here is the graveyard.
Candidate 1: Negative Impls (nightly)
Hey, remember “Rust has no negative trait bounds”? Well, nightly Rust kind of does:
#![feature(negative_impls)]
impl<T: NonFungibleEnumerable> !NonFungibleConsecutive for T {}
This is exactly the sentence we wanted to write in the first place! Extensions don’t need to pin the slot to a concrete type anymore, so AllowList and TotalSupply stop being falsely incompatible. Problem solved?
Sadly, it solves exactly half of the problem.
Negative impls tell the compiler what is forbidden. They don’t tell anyone what the combined behavior is. Say AllowList and TotalSupply are both allowed now. Both of them need to change how tokens move: one rejects unknown addresses, the other has to update the supply on mint and burn. But the behavior still dispatches through that one ContractType slot, and you can only put one type there. Which type? Neither AllowList nor TotalSupply alone does both things.
So we would still need a type that does both. Somebody has to write it. Keep this in mind, it will come back.
And the price for this half-solution: #![feature(...)] in a library crate forces every downstream user onto nightly, because dependencies are compiled with the user’s toolchain. “Hey, nice token library, by the way your whole project is on nightly now” is not the developer experience we are going for.

Moving on.
Candidate 2: Hooks
This one is the obvious one if you come from Solidity (or from any web framework ever). Forget about override slots. Have one base transfer, and let extensions plug into it with hooks (pre and post):
pub trait TransferHook {
fn before_transfer(e: &Env, from: &Address, to: &Address, amount: i128) {}
fn after_transfer(e: &Env, from: &Address, to: &Address, amount: i128) {}
}
AllowListchecks the addresses inbefore_transfer.Votesmoves voting power inafter_transfer.TotalSupplyupdates the supply around mint and burn.
Every extension minds its own business, and you can stack as many as you want. Composable by construction!
In our library though, things got awkward pretty quickly:
- The library gets locked into
pre + BASE + post. TheBASEin the middle can never be overridden.Consecutivedoesn’t want to run “a bit of extra code” around the basetransfer. The basetransferwrites one owner entry per token, andConsecutivedoesn’t have one owner entry per token. It needs to replace the whole base logic. Andowner_offorConsecutiveis a search over ranges, not a simple lookup. There is no “before” or “after” for a getter, it just answers differently. The way out is to promote things likeConsecutiveto a new kind of thing (a “substrate”, one per contract), which is one more concept for the developer to learn. And every future module that doesn’t fit the sandwich means another substrate, or another breaking change. Long term, every module has to be designed to fit the hook shape, instead of the hooks fitting the module. - You don’t see where your code runs. In practice, the hooks don’t sit around
transferitself, they sit around one shared internalupdatefunction thattransfer,transfer_from,mintandburnall go through (approvals get their own hook, just to keep you on your toes). Sounds efficient: write the check once, cover every path! Until the check that is perfectly correct fortransferturns out to be wrong formint. Nothing at the place you wrote it tells you which functions it affects. To find out, you go read the library internals. - Auditors don’t see it either. Same root cause, different victim. The inserted code is never shown in the context where it actually runs, so the auditor has to rebuild every
pre + BASE + postsandwich in their head, for every path. And a bug in a hook doesn’t break one function, it breaks all of them at once. - Weaker type safety. With an associated type, every valid variant of a contract is an explicit, named type, and the compiler checks against exactly that. With a bag of hooks, the compiler mostly checks that each hook exists, not that the combination makes sense.
- Post hooks want to know what happened before. Some extensions need the state before the transfer to do their work after it. So the pre hook has to pass something to the post hook, which means a context type threaded through every hook of every extension (
type Ctx = ();everywhere). Fun! - The order is now invisible. If
AllowListandVotesboth hook intotransfer, which one runs first? Does it matter? Today, maybe not. When someone adds the next extension, maybe yes. With hooks, the order is decided by whoever wires them, and as the library, we cannot provide any guarantees over it.

Candidate 3: A Builder
let token = ContractBuilder::new().with(AllowList).with(Votes).build();
Sounds wonderful at first sight. The extensions can be declared in any order, and the library can provide the safe implementations/overrides for every combinations.
The main problem is, it won’t work. A builder produces a value, at runtime. We need a type (to put into type ContractType = ...), at compile time. You can’t put a function call into an associated type.

Candidate 4: Combinatorics
Remember the origin of this discussion? Two or more extensions (types) cannot be combined in a single type slot for our ContractType.
What if we combine the possible combinations in a single type?
For each pair of extensions that is actually compatible, the library ships a concrete combined type: AllowListVotes, TotalSupplyAllowList, EnumerableVotes, and so on.
For pairs that are incompatible, the library ships nothing.
And instead of pinning the slot to one exact type, each extension asks for a capability, using a marker trait:
pub trait EnumerableContractType {}
impl EnumerableContractType for Enumerable {}
impl EnumerableContractType for EnumerableVotes {} // the combined type is enumerable compatible too
pub trait NonFungibleEnumerable: NonFungibleToken<ContractType: EnumerableContractType> {} // HERE! It is not `Enumerable` anymore. It is `EnumerableContractType`
Read that bound out loud: “my contract type must be Enumerable Contract Type”, not “my contract type must be Enumerable”. EnumerableVotes is enumerable compatible, it also has voting power, so both NonFungibleEnumerable and NonFungibleVotes can be implemented on the same contract. Consecutive is not enumerable compatible, and nobody ships a type that is both enumerable and consecutive, so that pair still doesn’t compile. Exactly the exclusions we wanted, and no more.
And here is the best part. The combined type is where the combined behavior lives, written by the library, in a safe, and audited way:
impl EnumerableVotes {
pub fn transfer(e: &Env, from: &Address, to: &Address, token_id: u32) {
Enumerable::transfer(e, from, to, token_id);
transfer_voting_units(e, Some(from), Some(to), 1);
}
}
Enumerable bookkeeping first, then voting units. The order is not an accident of wiring, it is right there, in a function, in the library, audited.
I really like this. Let’s score it:
- overrides: yes. Every combination has its own explicit behavior, including full replacements like
Consecutiveand query overrides likeowner_of. Nothing has to be squeezed into a “before” or “after”. - mutual exclusion: yes, and only for the pairs that are actually incompatible. A pair with no combined type simply doesn’t exist.
- auditable: the custom code sits inside the function where it runs.
transferistransfer, with all of its context right there. No sandwich to reconstruct. - idiomatic: it’s just traits, unit structs and associated types. Stable Rust.
And the sane API?
impl FungibleToken for MyToken {
type ContractType = TotalSupplyAllowBlockList;
}
- Was it
TotalSupplyAllowBlockListorAllowBlockListTotalSupply? - Is there a
CappedAllowBlockList? - What about
AllowListVotes, or is itVotesAllowList?

How would I even know which combinations exist, other than scrolling through the docs? And if I add a fourth extension next month, do I have to go and find the name of a new type that I’ve never heard of?
The mental model is great. The DevX is a guessing game about naming conventions.
So we have a solution that is type safe, transparent about behavior, and idiomatic, and the only thing wrong with it is the spelling. Fine. Let’s fix the spelling.
I’m warning you, it won’t be easy!
Compose
Here is a sneak peek to the solution:
What the developer actually wants to write is a list of ingredients. So let’s let them write a list of ingredients:
impl FungibleToken for MyToken {
type ContractType = Compose<(AllowList, FungibleVotes)>;
}
impl FungibleAllowList for MyToken {}
impl Votes for MyToken {}
That’s it. You list what your token is made of, and the compiler figures out that you meant AllowListVotes. You never see that name. You don’t need to know it exists. Order doesn’t matter either, (FungibleVotes, AllowList) resolves to the exact same type.
And the forbidden pair:
impl NonFungibleToken for MyContract {
type ContractType = Compose<(Enumerable, Consecutive)>;
}
gives you:
error[E0277]: `Enumerable` cannot be combined with `Consecutive` in a `Compose` list
|
41 | type ContractType = Compose<(Enumerable, Consecutive)>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the contract types are mutually exclusive, or no combination of them is curated
A compile error that says what went wrong, in human words. In Rust. With traits. I almost cried.

Under the hood, it’s still the combinatorics solution, all of it. The combined types are still there, the marker traits are still there, the hand-written combined behavior is still there. Compose only does one job: it takes the list you wrote, and finds the name of the right combined type. Let’s build it step by step.
Step 1: A Function, But For Types
What we need is, something like a function that takes (AllowList, FungibleVotes) and returns AllowListVotes. Except the input and the output are types, and it has to run at compile time. This means, it cannot be a function.
You might be surprised, but traits can do this for us!
First, we declare a trait with an associated type. That associated type will be our output:
pub trait Composable {
type Out;
}
The input is whatever type we implement the trait for. So read every impl of Composable as “input → output”. For example:
// input: `AllowList` → output: `AllowList`
impl Composable for AllowList {
type Out = AllowList;
}
A token made of just AllowList needs the AllowList contract type, nothing fancier. So the input and the output happen to be the same type here. That’s what this impl says. (For lists with more than one extension, the output will be a combined type, like AllowListVotes. We’ll get there.)
Ok, but how do we “call” this function with our input, and get the output back?
<AllowList as Composable>::Out is the call. The compiler finds the impl for AllowList, and gives you its Out, which is AllowList.
The syntax doesn’t look great, I agree. A type alias makes it much nicer. Compose is just a shorter name for that call:
pub type Compose<L> = <L as Composable>::Out;
Now Compose<AllowList> is AllowList. And when you write Compose<(AllowList, FungibleVotes)>, the compiler goes looking for impl Composable for (AllowList, FungibleVotes).
And “yes”, we can implement a trait for (AllowList, FungibleVotes)? A tuple of types is a type on its own.
Step 2: The Naive Way
The most straightforward implementation is to write down every list by hand:
impl Composable for (AllowList, FungibleVotes) {
type Out = AllowListVotes;
}
impl Composable for (FungibleVotes, AllowList) {
type Out = AllowListVotes;
}
It works. But a list of 3 extensions can be written in 6 different orders, a list of 4 in 24 orders… and that’s for one combination. Factorial!
Sounds scary, but honestly? This solution will actually suffice. One can even use a macro to generate all the orders, so nobody has to type them by hand. And the factorial only costs some compile time: these impls contain no code, just types, so your smart contract doesn’t get a single byte bigger.
If you understood everything up to this point, this is a good place to stop. You already know how Compose works.
The rest of the post is about polishing the experience, for both the developer using the library and the ones maintaining it. Here is what we are going to get:
- No factorial, no macro: the library describes what happens when two things meet, and the number of rows grows with the number of extensions, not with the number of possible orders.
- Errors that point at the problem: Specific errors such as: “
Enumerablecannot be combined withConsecutive”. - Optional Additive extensions:
Burnableand similar non-overriding extensions can be OPTIONALLY listed anywhere in the list, without multiplying the boilerplate code.
Step 3: No Factorial, No Macro
Before any code, let’s see what we are trying to gain here. Say we want to support a token made of AllowList, BlockList and Votes.
With the naive way, we write down every order this list can be written in:
AllowList,BlockList,VotesAllowList,Votes,BlockListBlockList,AllowList,VotesBlockList,Votes,AllowListVotes,AllowList,BlockListVotes,BlockList,AllowList
6 entries, and all of them say the exact same thing: “this is AllowBlockListVotes”.
With pairs, we don’t write down lists at all. We only write down what happens when one more extension is added to what we already have. The rows for two extensions (AllowList + BlockList = AllowBlockList) already exist in both approaches, so no need to account for them.
AllowBlockList + Votes = AllowBlockListVotesAllowListVotes + BlockList = AllowBlockListVotesBlockListVotes + AllowList = AllowBlockListVotes
3 rows instead of 6. Not impressed? Add a 4th extension: the naive way needs 24 entries for that token, pairs need 4. A 5th one: 120 vs 5. The naive way grows with the number of possible orders (factorial), pairs grow with the number of extensions (one row per “which one came last”).
Now, the code. Instead of describing whole lists, we describe what happens when two things meet. That’s another trait, with the second thing as a generic parameter:
pub trait Combine<B> {
type Out;
}
impl Combine<FungibleVotes> for AllowList {
type Out = AllowListVotes;
}
// same as above, but in the reverse order, for DevX
impl Combine<AllowList> for FungibleVotes {
type Out = AllowListVotes;
}
Read it as: “AllowList combined with FungibleVotes gives AllowListVotes”, in whichever order.
Think of all these impls together as a table: each impl is one row, answering one “X + Y = ?” question. If a pair has no row, that pair doesn’t combine. Enumerable + Consecutive? No row. That’s where the error from above comes from.
Step 4: Errors That Point at the Problem
We have a list of extensions to combine. If the combination is valid, we want the combined type. If the combination is invalid, we want an error that says which pair is the problem.
We walk the list the same way you’d add up 1 + 2 + 3: first 1 + 2, then add 3 to the intermediate result.
impl<A, B> Composable for (A, B)
where
A: Combine<B>, // there must be a combination for `A + B`
{
type Out = <A as Combine<B>>::Out; // the answer is that combination
}
Now, this part is important. If you skip it, you may get very confused in the next one.
Let’s go step by step. What does the where clause mean?
where
A: Combine<B>
It means A has to implement Combine<B>. In other words, there must be a Combine<B> implementation for A.
Since we already have this:
impl Combine<FungibleVotes> for AllowList {
type Out = AllowListVotes;
}
the where clause is satisfied for AllowList and FungibleVotes.
And what does this mean?
type Out = <A as Combine<B>>::Out;
There are 2 parts to this:
A as Combine<B>-> take theCombine<B>implementation provided for typeA,<something>::Out-> and use theOuttype from that implementation.
Now, take a look at this one again:
// `Combine<FungibleVotes>` implementation provided for `AllowList`
impl Combine<FungibleVotes> for AllowList {
type Out = AllowListVotes; // `Out` type for that implementation
}
So <AllowList as Combine<FungibleVotes>>::Out is AllowListVotes.
Now let’s put it all together, and play the compiler. We write:
type ContractType = Compose<(AllowList, FungibleVotes)>;
1. Expand the alias. Compose<L> is just <L as Composable>::Out, and our L is (AllowList, FungibleVotes):
type ContractType = <(AllowList, FungibleVotes) as Composable>::Out;
2. Fill in the generic implementation. There is no Composable implementation written specifically for (AllowList, FungibleVotes) anywhere in the library. But our generic impl<A, B> Composable for (A, B) works for any 2-element tuple, including this one. So the compiler takes it, with A as AllowList and B as FungibleVotes. Here is what it looks like with the blanks filled in (this concrete version only exists in the compiler’s head, not in the code):
impl Composable for (AllowList, FungibleVotes)
where
AllowList: Combine<FungibleVotes>,
{
type Out = <AllowList as Combine<FungibleVotes>>::Out;
}
3. Check the where clause. Is there a Combine<FungibleVotes> implementation for AllowList? Yes, that’s our row (combination) from Step 3. Satisfied.
4. Resolve the Out. <AllowList as Combine<FungibleVotes>>::Out is the Out type from that combination, which is AllowListVotes. Replace it:
impl Composable for (AllowList, FungibleVotes) {
type Out = AllowListVotes;
}
Look familiar? It’s exactly the naive implementation from Step 2. Except this time, we didn’t write it, the compiler did.
5. Done.
type ContractType = AllowListVotes;
And Compose<(Enumerable, Consecutive)>? Same steps, but at step 3 the compiler looks for a Combine<Consecutive> implementation for Enumerable, and there isn’t one. The where clause is not satisfied, and you get the error from the beginning of this section.
For a list of 3, we take the result of the first step, and combine it with the third one:
impl<A, B, C> Composable for (A, B, C)
where
A: Combine<B>, // there must be a combination for `A + B`
<A as Combine<B>>::Out: Combine<C>, // and a combination for `(A + B) + C`
{
type Out = <<A as Combine<B>>::Out as Combine<C>>::Out;
}
And yes, we need one impl like this for each list length (2, 3, 4, 5…). We stopped at 5, and nobody has complained yet.

If you understood the (A, B) one, this (A, B, C) one is not that different, actually.
The only line I’d like to clarify is this one:
<A as Combine<B>>::Out: Combine<C>,
First, remember what <A as Combine<B>>::Out is: the Out type from the Combine<B> implementation for A. Let’s just call it MyOut for simplicity, and we have instead:
MyOut: Combine<C>
And you already know what this means: that MyOut type has to implement Combine<C>. In other words, there has to be a Combine<C> implementation for MyOut.
Writing The Error Message
Now it’s time to demystify where this error message is coming from:
error[E0277]: `Enumerable` cannot be combined with `Consecutive` in a `Compose` list
|
41 | type ContractType = Compose<(Enumerable, Consecutive)>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the contract types are mutually exclusive, or no combination of them is curated
Rust has a stable attribute for it: #[diagnostic::on_unimplemented]. You put it on a trait, and when the compiler needs that trait but finds no implementation, it prints your message instead of the default one:
#[diagnostic::on_unimplemented(
message = "`{Self}` cannot be combined with `{B}` in a `Compose` list",
note = "the contract types are mutually exclusive, or no combination of them is curated"
)]
pub trait Combine<B> {
type Out;
}
The compiler fills in {Self} and {B}. No combination for Enumerable + Consecutive, so we get “Enumerable cannot be combined with Consecutive”. Lovely.
The catch: one message per trait. So a mistake that deserves its own message gets it on the trait where that mistake shows up. For example, an allowlist token that implements FungibleTotalSupply but forgets TotalSupply in its list fails on TotalSupplyOverrides, an internal trait the developer has never heard of. So that trait carries its own message:
error[E0277]: `AllowList` does not track the total supply, so `FungibleTotalSupply` cannot be implemented
= note: add `TotalSupply` to the `Compose` list, e.g. `Compose<(AllowList, TotalSupply)>`
It names the problem, and the fix.
Step 5: Optional Additive Extensions
Some extensions are purely additive. Burnable adds a burn function, Royalties adds royalty queries, but neither of them overrides anything. They don’t need a contract type at all.
But it would be super annoying if the developer had to remember which extensions override and which don’t. So we want to let the developer list any extension in Compose, overriding or not. Very convenient!
There is a problem though. Our table only has rows for extensions that override something. There is no Enumerable + Burnable row. And adding Burnable rows for every single entry in the table (and then Royalties rows, and then…) would bring back exactly the explosion we were running away from.
So, instead of adding rows, we add one small step before the table, and one small step after it. Three new things to learn: Extension (and Nil), Pair, and Finalize. One by one.
Extension: What Does This Entry Bring to the Table?
Before going to the table, each entry in the list is first translated into what it contributes:
pub trait Extension {
type Contribution;
}
Same trick as Composable in Step 1: a “function” from type to type. Input: the entry you listed. Output: its Contribution.
An extension that overrides something contributes itself:
impl Extension for Enumerable {
type Contribution = Enumerable; // contributes itself
}
And an extension that overrides nothing, contributes… nothing:
impl Extension for Burnable {
type Contribution = Nil; // contributes nothing
}
And, as in Step 4, writing <A as Extension>::Contribution everywhere gets old pretty fast, so we give it a shorter name:
type Contrib<A> = <A as Extension>::Contribution;
So Contrib<Enumerable> is Enumerable, and Contrib<Burnable> is Nil.
Nil: The Zero of Our Table
What is Nil? Just this:
pub enum Nil {}
An enum with no variants. You can’t even create a value of it. It doesn’t need values, we only ever use it as a type, to say “nothing here”.
Nil is the “zero” of our table: anything combined with Nil stays the same, just like x + 0 = x. One in each direction for every contract type:
impl Combine<Nil> for Enumerable {
type Out = Enumerable; // Enumerable + Nil = Enumerable
}
impl Combine<Enumerable> for Nil {
type Out = Enumerable; // Nil + Enumerable = Enumerable
}
impl Combine<Nil> for Nil {
type Out = Nil; // Nil + Nil = Nil
}
See the gain? Burnable, Royalties, and any additive extension we add in the future, all turn into the same Nil. So the table needs rows for Nil once, not for each of them separately. A new additive extension costs exactly one impl Extension ... { type Contribution = Nil; } and zero new rows.
Pair: Just a Shorter Name
Nothing new here, I promise. In Step 4, we used <A as Combine<B>>::Out for “the combination for A + B”. It’s going to show up a lot, so:
type Pair<A, B> = <A as Combine<B>>::Out;
So Pair<AllowList, FungibleVotes> is AllowListVotes, and Pair<Enumerable, Nil> is Enumerable.
Finalize: Making Sure We End Up With a Real Contract Type
Now, what if the developer lists only additive extensions? Compose<(Burnable,)>: Burnable contributes Nil, and there is nothing else in the list. The answer is Nil.
But Nil is not a contract type! Remember from the previous article: whatever goes into type ContractType = ... has to implement ContractOverrides, so it can provide the default behavior. Nil implements nothing.
What should the answer be? A token with just Burnable and no overrides is a plain vanilla token. So: Base.
That’s the last step, after the table:
pub trait Finalize {
type Out: ContractOverrides;
}
impl Finalize for Nil {
type Out = Base; // nothing overridden -> vanilla token
}
impl Finalize for Enumerable {
type Out = Enumerable; // already a contract type -> stays as is
}
// ... and the same "stays as is" row for every other contract type
Notice the Out: ContractOverrides bound. Whatever comes out of Finalize is guaranteed to be a real contract type, so Nil can never sneak into ContractType.
And, of course, a shorter name:
type Final<A> = <A as Finalize>::Out;
So Final<Nil> is Base, and Final<Enumerable> is Enumerable.
“All this machinery, just so Burnable can sit in a list and do nothing?”

All Together
Recap of the whole pipeline:
Extension: translate each entry into its contribution (Contrib).Combine: walk through the contributions, two at a time, using the table (Pair).Finalize: turn the result into a real contract type (Final).
And here is the real implementation for list of only 1 extensions:
impl<A> Composable for (A,)
where
A: Extension,
Contrib<A>: Finalize,
{
type Out = Final<Contrib<A>>;
}
And here is the real implementation for list of only 2 extensions:
impl<A, B> Composable for (A, B)
where
A: Extension,
B: Extension,
Contrib<A>: Combine<Contrib<B>>,
Pair<Contrib<A>, Contrib<B>>: Finalize,
{
type Out = Final<Pair<Contrib<A>, Contrib<B>>>;
}
Let’s read the (A, B) one, line by line:
A: Extension,B: Extension: both entries must be things that can appear in aComposelist (soCompose<(u32, bool)>doesn’t compile).Contrib<A>: Combine<Contrib<B>>: there must be a combination for “whatAcontributes + whatBcontributes”.Pair<Contrib<A>, Contrib<B>>: Finalize: the result of that combination must be somethingFinalizecan turn into a contract type.type Out = Final<Pair<Contrib<A>, Contrib<B>>>: read it inside out. Translate both entries, combine them, finalize the result.
Same thing as Step 4, wrapped between a “before” (Contrib) and an “after” (Final).
And the (A,) one is the same pipeline, minus the table: there is only one entry, so nothing to combine. Translate it, finalize it.
Let’s play the compiler one more time, with Compose<(Enumerable, Burnable)>.
1. Expand the alias:
type ContractType = <(Enumerable, Burnable) as Composable>::Out;
2. Fill in the generic implementation. Same as before: no implementation is written for (Enumerable, Burnable) specifically, so the compiler takes the generic impl<A, B> Composable for (A, B), with A as Enumerable and B as Burnable, and fills in the blanks:
impl Composable for (Enumerable, Burnable)
where
Enumerable: Extension,
Burnable: Extension,
Contrib<Enumerable>: Combine<Contrib<Burnable>>,
Pair<Contrib<Enumerable>, Contrib<Burnable>>: Finalize,
{
type Out = Final<Pair<Contrib<Enumerable>, Contrib<Burnable>>>;
}
3. Resolve the contributions. Contrib<Enumerable> is Enumerable, and Contrib<Burnable> is Nil. Replace them:
impl Composable for (Enumerable, Burnable)
where
Enumerable: Extension,
Burnable: Extension,
Enumerable: Combine<Nil>,
Pair<Enumerable, Nil>: Finalize,
{
type Out = Final<Pair<Enumerable, Nil>>;
}
4. Resolve the pair. Is there a Combine<Nil> implementation for Enumerable? Yes. Its Out is Enumerable, so Pair<Enumerable, Nil> is Enumerable. Replace it:
impl Composable for (Enumerable, Burnable)
where
Enumerable: Extension,
Burnable: Extension,
Enumerable: Combine<Nil>,
Enumerable: Finalize,
{
type Out = Final<Enumerable>;
}
5. Finalize. Is there a Finalize implementation for Enumerable? Yes, the “stays as is” one. So Final<Enumerable> is Enumerable. All where clauses are satisfied, and we have:
impl Composable for (Enumerable, Burnable) {
type Out = Enumerable;
}
6. Done.
type ContractType = Enumerable;
The Burnable simply vanished. Exactly what we wanted: it doesn’t override anything, so it doesn’t change the contract type.
And for Compose<(Burnable,)>, the (A,) implementation goes: Contrib<Burnable> is Nil, Final<Nil> is Base. A vanilla token.
Hiding the Machinery
Extension, Combine, Finalize, Nil… the developer should never see, import, or implement any of these. They live in a private module, and only Compose (and Composable, for the error messages) is exposed:
pub type Compose<L> = <L as Composable>::Out;
mod fold {
pub trait Extension { type Contribution; }
pub trait Combine<B> { type Out; }
pub trait Finalize { type Out: crate::fungible::ContractOverrides; }
pub enum Nil {}
}
pub items inside a private module: public enough to appear in public bounds, unnameable from outside the crate. Nobody can add their own rows (combinations) to our table and accidentally create an EnumerableConsecutive abomination.
Is It Worth It?
Let’s score one last time.
- overrides: every combination has explicit, hand-written, auditable behavior. Including full replacements and query overrides, the exact things hooks choked on.
- mutual exclusion: a pair is forbidden if and only if nobody wrote a combined type for it. No false conflicts anymore.
- sane API: list your extensions in any order. And if you list something silly, the compiler tells you why, in English.
- stable Rust: no nightly, no proc macros, no runtime cost. It’s all associated types, resolved at compile time.
The cost is on us, the maintainers: every new compatible pair is a new type and a couple of new rows in the table. Same deal as last time: the complexity moved into the library, away from the developer writing the token. And honestly, writing those combined types is a great design. Somebody has to decide what AllowList + Votes means. Better it’s us, once, than every developer, every time.
Here’s the whole thing from the developer’s side:
impl FungibleToken for MyToken {
type ContractType = Compose<(AllowList, FungibleVotes, Burnable)>;
/* NO METHODS NEEDED! */
}
impl FungibleAllowList for MyToken {}
impl Votes for MyToken {}
impl FungibleBurnable for MyToken {}
and I think that is (still) beautiful.