Implementation:1ay1 Agentty Refinement Types
| Knowledge Sources | |
|---|---|
| Domains | Type_System, Validation |
| Last Updated | 2026-10-28 12:00 GMT |
Overview
Refinement type template `Refined<T, Predicate>` carrying a type-level proof that a predicate holds, making invalid states unrepresentable at construction time.
Description
The agentty::domain::Refined<T, Predicate> template is the C++ analogue of Rust's "make invalid states unrepresentable" via newtypes plus smart constructors. A `Refined<T, Predicate>` is a value of type T carrying a type-level proof that some predicate holds. Once constructed, downstream code can read the inner value without re-checking — the only way to construct one is through `try_make`, which validates and returns an `expected`.
The predicate is a stateless functor with `static constexpr bool check(const T&)` and `static constexpr std::string_view what()`. The same predicate type is the identity of the refinement: `NonEmpty<string>` and `NotBlank<string>` are different types even though both wrap a `string`.
Provided predicates: `NonEmpty` (non-empty container), `Bounded<T, Lo, Hi>` (integral in range), `Positive<T>` (>0), `NonNegative<T>` (>=0), `NonBlank` (non-empty + non-whitespace), `MaxLen<N>` (size <= N). A `consteval` overload of `try_make` exists for compile-time-checked literals.
Usage
Import this when you need to: accept a validated value at an API boundary, enforce invariants at the type level, or avoid re-validation at every call site.
Code Reference
Source Location
- Repository: 1ay1_Agentty
- File: include/agentty/domain/refined.hpp
- Lines: 1-251
Signature
namespace agentty::domain {
template <class P, class T>
concept RefinementPredicate = requires(const T& v) {
{ P::check(v) } -> std::convertible_to<bool>;
{ P::what() } -> std::convertible_to<std::string_view>;
};
template <class T, class Predicate>
requires RefinementPredicate<Predicate, T>
class Refined {
T value_;
explicit constexpr Refined(T v) noexcept;
public:
using value_type = T;
using predicate = Predicate;
[[nodiscard]] constexpr const T& value() const noexcept;
[[nodiscard]] constexpr operator const T&() const noexcept;
[[nodiscard]] constexpr operator std::string_view() const noexcept
requires std::convertible_to<const T&, std::string_view>;
struct Error { std::string_view what; std::string detail; };
[[nodiscard]] static constexpr std::expected<Refined, Error>
try_make(T v, std::string_view detail_context = {});
[[nodiscard]] static consteval Refined make_unchecked(T v);
};
// Provided predicates:
template <class T> using NonEmpty = Refined<T, NonEmptyPred>;
template <class T, T Lo, T Hi> using Bounded = Refined<T, InRangePred<T, Lo, Hi>>;
template <class T> using Positive = Refined<T, PositivePred<T>>;
template <class T> using NonNegative = Refined<T, NonNegativePred<T>>;
template <class T> using NonBlank = Refined<T, NonBlankPred>;
template <class T, std::size_t N> using MaxLen = Refined<T, MaxLenPred<N>>;
}
Import
#include "agentty/domain/refined.hpp"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| T | type | Yes | The inner value type (string, int, container) |
| Predicate | functor | Yes | Stateless predicate with static check() and what() |
| v | T | Yes (try_make) | The value to validate and wrap |
Outputs
| Name | Type | Description |
|---|---|---|
| try_make() | std::expected<Refined, Error> | Success with validated value, or Error with predicate reason |
| value() | const T& | The inner validated value (no re-check needed) |
Usage Examples
Validate at API Boundaries
#include "agentty/domain/refined.hpp"
using agentty::domain::NonEmpty;
using agentty::domain::NonBlank;
// The only way in is through try_make — validation at construction
auto result = NonEmpty<std::string>::try_make(user_input);
if (!result) {
// result.error().what == "must be non-empty"
return error(result.error().what);
}
auto name = *result; // NonEmpty<std::string> — no re-check downstream
// Stricter: NonBlank rejects all-whitespace
auto blank = NonBlank<std::string>::try_make(" ");
// blank.has_value() == false
Bounded Integers
using agentty::domain::Bounded;
using agentty::domain::Positive;
auto port = Bounded<int, 0, 65535>::try_make(8080); // OK
auto bad = Bounded<int, 0, 65535>::try_make(99999); // error
auto count = Positive<int>::try_make(1); // OK
auto zero = Positive<int>::try_make(0); // error: must be > 0
Compile-Time Literals
// consteval factory — compile error if the literal fails the predicate
constexpr auto name = agentty::domain::NonEmpty<std::string>::make_unchecked("hello");
// constexpr auto bad = agentty::domain::NonEmpty<std::string>::make_unchecked(""); // won't compile