Added self views of the interface in makeExtensibleWithInterface

Fixing the `overrideScope` in `haskellpackages`.
This commit is contained in:
Will Fancher
2017-07-14 06:43:33 -04:00
parent 8b764960e9
commit 05f9db601a
5 changed files with 76 additions and 64 deletions
+24 -27
View File
@@ -50,28 +50,22 @@ rec {
}
else { }));
# Like `makeOverridable`, except a `self` argument is passed to `f`,
# which represents the fixed point result, even after using `extend`
# or `override`.
#
# Also, an `interface` function is taken as an argument, paralleling
# the `interface` argument to `makeExtensibleWithInterface`. This Is
# mostly useful for adding new `override` style functions,
# e.g. `overrideScope`.
# A more powerful version of `makeOverridable` with features similar
# to `makeExtensibleWithInterface`.
makeOverridableWithInterface = interface: f: origArgs: let
addOverrideFuncs = {val, args, ...}: overridePackage:
(lib.optionalAttrs (builtins.isAttrs val) (val // {
extend = f: overridePackage (self: super: {
extend = f: overridePackage (_: self: super: {
val = super.val // f self.val super.val;
});
overrideDerivation = newArgs: overridePackage (self: super: {
overrideDerivation = newArgs: overridePackage (_: self: super: {
val = lib.overrideDerivation super.val newArgs;
});
${if val ? overrideAttrs then "overrideAttrs" else null} = fdrv:
overridePackage (self: super: {
overridePackage (_: self: super: {
val = super.val.overrideAttrs fdrv;
});
})) // (lib.optionalAttrs (builtins.isFunction val) {
@@ -81,15 +75,15 @@ rec {
}) // {
inherit overridePackage;
override = newArgs: overridePackage (self: super: {
override = newArgs: overridePackage (_: self: super: {
args = super.args //
(if builtins.isFunction newArgs then newArgs super.args else newArgs);
});
};
in lib.makeExtensibleWithInterface (x: o: interface (addOverrideFuncs x o) o) (self: {
in lib.makeExtensibleWithInterface (x: o: interface (addOverrideFuncs x o) o) (output: self: {
args = origArgs;
val = f self.args self.val;
val = f output self.args self.val;
});
@@ -145,35 +139,37 @@ rec {
it exposes a deeper structure. It provides `self` and `super`
views of both the arguments and return value of the function,
allowing you to change both in one override; you can even have
overrides for one based on overrides for the other. The type of
`self`, `super`, and the return value are all:
`{ args :: argumentsToF, val :: returnValueOfF }`
overrides for one based on overrides for the other. It also
provides the `output` view, which is the view of `self` after
passing it through the `makeOverridable` interface and adding all
the `overrideX` functions. `output` is necessary when your
overrides depend on the overridable structure of `output`.
nix-repl> obj = makeOverridable ({a, b}: {inherit a b;}) {a = 1; b = 3;}
nix-repl> obj = obj.overridePackage (self: super: { args = super.args // {b = self.val.a;}; })
nix-repl> obj = obj.overridePackage (output: self: super: { args = super.args // {b = self.val.a;}; })
nix-repl> obj.b
1
nix-repl> obj = obj.overridePackage (self: super: { val = super.val // {a = self.args.a + 10;}; })
nix-repl> obj = obj.overridePackage (output: self: super: { val = super.val // {a = self.args.a + 10;}; })
nix-repl> obj.b
11
*/
makeOverridable = fn: makeOverridableWithInterface (x: _: x) (args: _: fn args);
makeOverridable = fn: makeOverridableWithInterface (x: _: x) (_: args: _: fn args);
callPackageCommon = functionArgs: scope: f: args:
let
intersect = builtins.intersectAttrs functionArgs;
interface = val: overridePackage: val // {
overrideScope = newScope: overridePackage (self: super: {
overrideScope = newScope: overridePackage (_: self: super: {
scope = super.scope.extend newScope;
});
};
in (makeOverridableWithInterface interface f (intersect scope // args))
.overridePackage (self: super: {
.overridePackage (output: self: super: {
inherit scope;
# Don't use super.args because that contains the original scope.
args = intersect self.scope // args;
@@ -219,15 +215,16 @@ rec {
*/
callPackageWith = autoArgs: fn: args:
let f = if builtins.isFunction fn then fn else import fn;
in callPackageCommon (builtins.functionArgs f) autoArgs (x: _: f x) args;
in callPackageCommon (builtins.functionArgs f) autoArgs (output: x: _: f x) args;
# Like `callPackageWith`, but provides the function with the `self`
# argument. `fn` is called with the new `self` whenever an override
# Like `callPackageWith`, but provides the function with a `self`
# view of the output, which has the override functions
# injected. `fn` is called with the new output whenever an override
# or extension is added.
callPackageWithSelfWith = autoArgs: fn: args:
callPackageWithOutputWith = autoArgs: fn: args:
let f = if builtins.isFunction fn then fn else import fn;
in callPackageCommon (builtins.functionArgs f) autoArgs f args;
in callPackageCommon (builtins.functionArgs f) autoArgs (output: args: _: f args output ) args;
/* Like callPackage, but for a function that returns an attribute
+29 -14
View File
@@ -71,19 +71,34 @@ rec {
# Same as `makeExtensible` but the name of the extending attribute is
# customized.
makeExtensibleWithCustomName = extenderName: makeExtensibleWithInterface
(fixedPoint: extend: fixedPoint // { ${extenderName} = extend; });
makeExtensibleWithCustomName = extenderName: f: makeExtensibleWithInterface
(fixedPoint: extend: fixedPoint // { ${extenderName} = ext: extend (_: ext); })
(_: f);
# Similar to `makeExtensible`, but expects you to implement the
# final interface for the result. Specifically, it takes an extra
# argument: a function that takes the final result and the `extend`
# function as arguments, and returns a transformed result
# (preferably one that contains the `extend` function). This is
# mainly useful for getting to choose what to name the `extend`
# function in the resulting attribute set. But it's also useful for
# having an internal structure that extensions can see, but the user
# facing code cannot.
makeExtensibleWithInterface = interface: fext: interface
(fix' fext)
(f: makeExtensibleWithInterface interface (extends f fext));
# A version of `makeExtensible` that allows the function being fixed
# to return a different interface than the interface returned to the
# user. Along with `self` and `super` views of the internal
# interface, a `self` view of the output interface is also
# provided. `extend` is not added to the output by default. This is
# the job of the interface.
#
# nix-repl> foo = {a, b}: {c = a + b;}
#
# nix-repl> interface = {args, val, ...}: extend: val // {inherit extend;}
#
# nix-repl> obj = makeExtensibleWithInterface interface (output: self: { args = {a = 1; b = 2;}; val = foo self.args; })
#
# nix-repl> obj.c
# 3
#
# nix-repl> obj = obj.extend (output: self: super: { args = super.args // { b = output.d; }; })
#
# nix-repl> obj = obj.extend (output: self: super: { val = super.val // { d = 10; }; })
#
# nix-repl> { inherit (obj) c d; }
# { c = 11; d = 10; }
makeExtensibleWithInterface = interface: f: let i = interface
(fix' (f i))
(fext: makeExtensibleWithInterface interface (i': (extends (fext i') (f i'))));
in i;
}