digitalmars.D - Amazing brand spanking new programming concept: Parameter Paths!
- TheHamster (49/49) Aug 20 2015 Parameter paths, a thousand words summed up:
- John Colvin (3/6) Aug 21 2015 Could you give an example of where this enables something really
- TheHamster (7/15) Aug 21 2015 Probably not, just like a new born baby, it takes time to grow in
- BBasile (11/15) Aug 21 2015 unfortunately, with std.traits a template can be shorter and it
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!
Aug 20 2015
On Friday, 21 August 2015 at 00:29:35 UTC, TheHamster wrote:Parameter paths, a thousand words summed up: void foo(p1, p2|p3|p4, p5|p6, |*p7|p8){ ... } [...]Could you give an example of where this enables something really new and/or much more convenient than using templates?
Aug 21 2015
On Friday, 21 August 2015 at 09:21:48 UTC, John Colvin wrote:On Friday, 21 August 2015 at 00:29:35 UTC, TheHamster wrote:Probably not, just like a new born baby, it takes time to grow in to something useful. Templates used to be a baby too. (BTW, the first parameter for foo should be p1|p1|p1) I suspect that parameter paths do not offer much more over templated functions except convince in a few cases.Parameter paths, a thousand words summed up: void foo(p1, p2|p3|p4, p5|p6, |*p7|p8){ ... } [...]Could you give an example of where this enables something really new and/or much more convenient than using templates?
Aug 21 2015
On Friday, 21 August 2015 at 00:29:35 UTC, TheHamster wrote:Parameter paths, a thousand words summed up: [...] Essentially it could reduce overhead of writing common sets of overloaded functions.unfortunately, with std.traits a template can be shorter and it will cover more type (all built-in numeric type, so natively comparable) [Templates] T Max(T a, T b) if (isNumeric!T) { return (a < b) ? b : a; } vs [Parameter Paths] auto Max(int a|float a, int b|float b) { return (a < b) ? b : a; } And by the way with a TypeTuple (oh sorry...an AliasSeq...) there are also other options to write a short constraint.
Aug 21 2015