Smart contracts frequently encounter scenarios where values exist in multiple distinct variants. A payment mechanism might need to process native Ethereum, an ERC20 token transfer, or an NFT transfer. Similarly, a decentralized auction contract might transition through states such as unstarted, active, ended, or cancelled. Handling these diverse variants traditionally requires splitting business logic across numerous functions throughout a codebase. When a developer introduces a new variant and inadvertently overlooks a single function, the compiler fails to flag the omission. Once deployed to a blockchain network, discovering and patching such oversights can incur substantial financial costs and operational risks.
Core Solidity addresses this persistent category of software bugs at compile time by introducing algebraic data types and robust pattern matching capabilities, building upon concepts explored in recent developer deep dives. This language evolution aims to fundamentally alter how smart contract developers model complex data structures, ensuring that safety properties once left to manual discipline and expensive post-deployment audits are mathematically guaranteed before bytecode ever hits the Ethereum Virtual Machine.
Why Classic Solidity Needs Better Data Modeling
Classic Solidity has long relied on a combination of enums and structs to model complex, mutually exclusive states. Consider a multi-asset payment processor designed to handle native ETH, ERC20 tokens, and ERC721 non-fungible tokens. In standard Solidity, a developer typically defines a payment type enumeration alongside a sprawling struct that contains fields for every possible parameter across all asset types, including token contract addresses, sender addresses, recipient addresses, amounts, and token identifiers.
This design introduces several critical safety challenges that the traditional compiler cannot detect. First, invalid states remain entirely representable. A single struct instance can easily be constructed with contradictory field values, such as a native ETH payment carrying a non-zero token address or an ERC721 transfer missing a mandatory token identifier. Because the type system itself lacks the expressiveness to prevent these incoherent combinations, developers are forced to rely heavily on runtime require statements. These checks must be manually written and painstakingly repeated across every single function that accepts the payment structure.
Furthermore, classic Solidity lacks compile-time totality guarantees. If a project expands its supported asset types to include a new standard like ERC1155, the compiler will not notify the developer of where dispatch logic must be updated. Functions processing payments or calculating fees will silently skip or improperly handle the new variant, failing quietly on-chain. This structural disconnect between the tag—such as the payment type enum—and its corresponding payload fields leaves room for human error during routine code refactoring and feature expansion.
Algebraic Data Types Make Invalid States Unrepresentable
Core Solidity solves these structural vulnerabilities by introducing algebraic data types that allow developers to define types expressing exact variants without superfluous fields. Using Core Solidity, a developer can define primitive wrapper types and build a precise payment sum type where each constructor carries strictly the data it requires—no more, no less.
Under this model, a native payment holds only a recipient and an amount, an ERC20 payment holds a token contract address, sender, recipient, and amount, while an ERC721 payment replaces the generic amount with a specific token identifier. It becomes structurally impossible to construct a payment value that pairs an ERC721 token identifier with a native payment designation because the underlying type system simply prohibits such configurations.
Consequently, payment processing functions shift from verbose conditional chains to declarative, self-documenting pattern matches. The pattern matcher binds precisely the fields relevant to each variant, eliminating the need for repetitive structural invariant checks. Because distinct variants live in separate constructors, fields like raw amounts and token identifiers cannot be accidentally confused. If a developer later introduces a new variant to the data type, the compiler immediately flags every match statement across the codebase that fails to handle the new case, preventing silent failures.
Totality and Exhaustiveness as Core Safety Properties
The primary safety property delivered by this architecture is exhaustiveness checking, often referred to as totality. A pattern match is deemed exhaustive when every possible value of the matched expression is handled by at least one branch. The Core Solidity compiler enforces this requirement statically, automatically rejecting any program containing an incomplete match.
Smart contract immutability elevates the stakes of code correctness significantly. Once a contract is deployed, modifying its core logic typically requires complex upgradeable proxy patterns, and any funds locked inside a flawed contract can remain permanently inaccessible. In classic Solidity, developers frequently rely on catch-all fallback branches or dead-code return statements simply to satisfy compiler requirements, leaving room for silent arithmetic or logical errors when new variants are added.
Exhaustiveness checking transforms these latent runtime bugs into immediate compile-time errors. When a developer omits a variant in a multi-state contract, the compiler generates a precise error message naming the missing case—known as a witness—to guide the developer directly to the unhandled code path. In addition to exhaustiveness, the compiler performs redundancy checking, issuing warnings when a specific pattern branch can never be reached because an earlier branch already encompasses its inputs, thereby catching copy-paste errors and leftover refactoring artifacts.
This compile-time rigor transforms the dynamics of smart contract security audits. Auditors traditionally spend considerable time manually tracing complex if-else chains across enum-heavy contracts to verify that every dispatch function accounts for every variant without falling through to silent defaults. Pattern matching with exhaustiveness checking renders this entire category of manual audit verification obsolete. If a contract compiles successfully, an auditor has an automated guarantee that all variants are handled, allowing security professionals to concentrate their efforts and client budgets on higher-value architectural findings.
The Pattern Match Compiler and Efficient Lowering
To achieve these guarantees without sacrificing performance, the Core Solidity prototype incorporates a dedicated compilation pass that translates nested patterns into efficient decision trees, following established functional programming compilation research. This approach makes exhaustiveness checking computationally tractable while producing clean, readable intermediate representations.
The pattern match algorithm evaluates match arms as a pattern matrix, selecting the most informative columns using necessity heuristics to construct sub-matrices recursively. Both exhaustiveness and redundancy emerge naturally from this matrix construction process, bypassing the need for separate, disconnected analysis passes. Once pattern compilation concludes, programs undergo monomorphization and are emitted into a first-order functional intermediate representation containing sum and product types before being translated into Yul.
A common apprehension regarding advanced type-system features is whether they impose hidden runtime overhead. Core Solidity avoids this penalty entirely through its lowering mechanism. Because Yul lacks native algebraic data types and operates strictly on 256-bit words, sum types are flattened onto the evaluation stack as a combination of a tag word and payload slots sized to accommodate the widest constructor.
For binary sums, boolean flags represent constructors directly, occupying minimal stack space. N-ary sums utilize integer tags accompanied by payload fields padded to match the largest variant in the group. Single-constructor wrapper types undergo total erasure, resulting in zero runtime overhead compared to raw primitive variables. Ultimately, the pattern matching constructs compile down to standard Yul switch statements matching precisely the code a meticulous developer would write by hand, ensuring that advanced safety guarantees are achieved at zero additional cost to on-chain execution gas.

