2 # Simple internal type checks for meta.
3 # This file is not a stable interface and may be changed arbitrarily.
5 # TODO: add a method to the module system types
6 # see https://github.com/NixOS/nixpkgs/pull/273935#issuecomment-1854173100
8 inherit (builtins) isString isInt isAttrs isList all any attrValues isFunction isBool concatStringsSep isFloat;
9 isTypeDef = t: isAttrs t && t ? name && isString t.name && t ? verify && isFunction t.verify;
17 str = self.string; # Type alias
49 attrsOf = t: assert isTypeDef t; let
52 name = "attrsOf<${t.name}>";
54 # attrsOf<any> can be optimised to just isAttrs
55 if t == self.any then isAttrs
56 else attrs: isAttrs attrs && all verify (attrValues attrs);
59 listOf = t: assert isTypeDef t; let
62 name = "listOf<${t.name}>";
64 # listOf<any> can be optimised to just isList
65 if t == self.any then isList
66 else v: isList v && all verify v;
69 union = types: assert all isTypeDef types; let
70 # Store a list of functions so we don't have to pay the cost of attrset lookups at runtime.
71 funcs = map (t: t.verify) types;
73 name = "union<${concatStringsSep "," (map (t: t.name) types)}>";
74 verify = v: any (func: func v) funcs;