Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib.asserts.assertAll: init #215166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

infinisil
Copy link
Member

@infinisil infinisil commented Feb 7, 2023

Description of changes

Split off from #209099 where this function is useful. It can also be used for #209375

This work is sponsored by Antithesis

Things done
  • Decided on the design
  • Wrote docs
  • Wrote tests

construct an error message.

Type:
assertAll :: (a -> Bool) -> [a] -> (Int -> a -> String) -> Bool
Copy link
Member Author

@infinisil infinisil Feb 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Design decisions:

  1. Should it be called with named arguments instead? E.g.

    # assertAll :: {
    #   condition :: a -> Bool;
    #   list :: [a];
    #   message :: Int -> a -> String;
    # } -> Bool
    assert assertAll {
      condition = n: n != 2;
      list = [ 1 3 5 ];
      message = i: n: "Index ${toString i}, value ${toString n} failed.";
    };

    RFC 135 also goes in that direction

  2. Should the message have access to not only the first, but all elements that don't satisfy the condition? The module system has such a use case:

    nixpkgs/lib/modules.nix

    Lines 765 to 767 in 12053de

    if all (def: type.check def.value) defsFinal then type.merge loc defsFinal
    else let allInvalid = filter (def: ! type.check def.value) defsFinal;
    in throw "A definition for option `${showOption loc}' is not of type `${type.description}'. Definition values:${showDefs allInvalid}"

    How should this look like? Maybe

    # assertAll :: {
    #   condition :: a -> Bool;
    #   list :: [a];
    #   message :: [{ index :: Int; value :: a; }] -> String;
    # } -> Bool
    assert assertAll {
      condition = n: n != 2;
      list = [ 1 2 3 ];
      message = failingElements: "Index ${(head failingElements).index}, value ${(head failingElements).value} failed.";

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Yes, and to make it like RFC 135, it should be
assertAll = {success, message}: list: ...
  1. Sounds good. Possibly with an index attached? Needs more thinking and discussion.

@infinisil infinisil mentioned this pull request Feb 7, 2023
2 tasks
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10 labels Feb 7, 2023
@roberth roberth added the 6.topic: lib The Nixpkgs function library label Jul 8, 2023
@wegank wegank added 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 2.status: merge conflict This PR has merge conflicts with the target branch labels Mar 19, 2024
@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Mar 20, 2024
@wegank wegank added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.status: merge conflict This PR has merge conflicts with the target branch 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 6.topic: lib The Nixpkgs function library 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants