Amazing brand spanking new programming concept: Parameter Paths!
TheHamster via Digitalmars-d
digitalmars-d at puremagic.com
Thu Aug 20 17:29:34 PDT 2015
Parameter paths, a thousand words summed up:
void foo(p1, p2|p3|p4, p5|p6, |*p7|p8){ ... }
Parameter paths provide a very simple way to express multiple
parameter paths for a function.
It is similar but not the same as operator overloading.
Essentially, a symbol such as '|' is used to separate possible
parameter paths in the signature of the function.
foo is essentially represented by the following functions:
void foo1(p1, p2, p5)
void foo4(p1, p3, p6, p7)
void foo4(p1, p8)
Writing foo a bit more verbose makes this clear:
void foo(|p1| | |, |p2|p3|p4|, |p5|p6| |, | |*p7|p8|)
which, if we associate entries,
|p1| | |
|p2| p3|p4|
|p5| p6| |
| |*p7|p8|
Whats interesting is that one can see these as matrix like
functions(On the input space, rather than vectors as usual).
For this to work for functions. Each "path" must provide a
distinct/unique "type" path. This is easily verifiable by the
compiler as it can easily work out the type path(which is just
the parameter paths but with types).
In some cases this allows one to present fewer arguments to the
user.
e.g., instead of foo(p1,p2,p3,p4,p5,p6,p7,p8) we can use any of
the "alias" above.
Essentially it could reduce overhead of writing common sets of
overloaded functions.
e.g.,
[Overloading]
int Max(int a, int b) {...}
float Max(float a, float b) {...}
vs
[Templates]
T Max(T a, T b) if (is(T == float) || is(T == int) { return (a <
b) ? b : a; }
vs
[Parameter Paths]
auto Max(int a|float a, int b|float b) { return (a < b) ? b : a; }
Note that no constraints are needed, and only one function is
required. Also note has much easier it is to maintain the
Parameter Path'ed version. If one wants to add the ability to use
doubles, it requires two changes, not one.
Note that with parameter paths, the same parameter name can be
used since different types produce different functions.
I'm sure it's not worth the trouble of adding to D but maybe it
will lead to bigger and brighter things!
More information about the Digitalmars-d
mailing list