Have fun: std::function without null#15360
Merged
Merged
Conversation
xokdvium
requested changes
Feb 27, 2026
xokdvium
reviewed
Feb 27, 2026
xokdvium
reviewed
Feb 27, 2026
xokdvium
reviewed
Feb 27, 2026
Like `ref<T>` guarantees a non-null pointer, `fun<Sig>` guarantees a non-null callable. When a callable is nullable, use `std::function` directly — not `optional<fun<>>`. The two types serve complementary roles: `fun<Sig>` for non-null, `std::function<Sig>` for nullable. Construction from callables (lambdas, function pointers) is implicit since these are inherently non-null. Construction from `std::function` is explicit, since it may be empty. `fun(nullptr)` is a deleted overload. A moved-from `fun<>` violates its non-null invariant, matching the precedent set by `ref<T>`. Deleting moves was considered but deemed unnecessary: the goal of this type is to document and enforce an invariant, not to force non-null at all costs including performance. `get_fn()` exposes the underlying `std::function` for API that only exists there, such as `target<>()`.
- `fun` was used for variable/field names in a number of places. These have been replaced by similar or more descriptive names, to avoid name shadowing, which is not allowed in the GCC-based build. - Use in-place construction instead of assignment. - `FilteringSourceAccessor::checkAccess` had a fallback for when `makeNotAllowedError` was null. Since `MakeNotAllowedError` is now `fun<>`, we've proven the null branch is dead code and have removed it. - `src/nix/flake.cc`, `src/nix/search.cc`, `src/nix/ls.cc` are left as-is — the self-referential lambdas there cause too much reindentation for too little benefit.
xokdvium
approved these changes
Feb 27, 2026
Contributor
xokdvium
left a comment
There was a problem hiding this comment.
A bit churny but oh well.
Ericson2314
approved these changes
Feb 27, 2026
brittonr
pushed a commit
to brittonr/nix
that referenced
this pull request
Apr 1, 2026
Have `fun`: `std::function` without null
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Tracking which functions may or may not be null is not my idea of fun.
This applies the same pattern we have with
ref.Side-catch:
1 line of dead code removed.
Occasional line reductions thanks to the shorter name, and a couple direct initializations. net: -9
Context
I've kept the puns to a minimum.
See commit messages for details.
Add 👍 to pull requests you find important.
The Nix maintainer team uses a GitHub project board to schedule and track reviews.