UFCS for arguments other than first?

Heisenberg via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 11 10:33:09 PST 2016


On Friday, 11 November 2016 at 17:07:50 UTC, John Colvin wrote:
> I'm sure there are loads of corner cases this doesn't cover, 
> but:
>
> template inPos(uint n, alias f)
> {
>     auto inPos(Args...)(auto ref Args args)
>     {
>         return f(args[1 .. n+1], args[0], args[n+1 .. $]);
>     }
> }
>
> import std.stdio;
> alias formatSecond = inPos!(1, writefln);
>
> void main()
> {
>
>     1.writeln(2,3,4,5);
>     1.inPos!(2, writeln)(2,3,4,5);
>
>     42.formatSecond("The answer is %d");
> }
>
> and of course you could easily develop more powerful templates 
> that could do any other order manipulations you like.

That might be a solution as well. Specifying the position near 
the variable will make the code much more straightforward to read 
though, while not forcing the programmer to declare separate 
templates in order to specify the position of a function's 
argument..


More information about the Digitalmars-d mailing list