Workaround for DIP 1005

Nick Treleaven via Digitalmars-d digitalmars-d at puremagic.com
Fri Feb 10 08:17:47 PST 2017


On Thursday, 9 February 2017 at 05:40:01 UTC, Jonathan M Davis 
wrote:
> auto func(alias pred, R)(R range)
>     if(from!"std.range.primitives".isInputRange!R &&
>        is(typeof(pred(range.front)) == bool))
> {...}
>
> but you can't import std.range.primitives.front to make dynamic 
> arrays work, whereas with DIP 1005, you can just add the import 
> to the function declaration, and it will work without having to 
> tie the import to a specific symbol.

That's a problem with UFCS in general. Workaround:

import std.functional;
import std.range;

void main(){
	auto r = [1];
	auto i = r.unaryFun!(std.range.front);
	assert(i == 1);
}

As an enhancement, it seems supporting value.(callable) would 
work:

range.(std.range.front)

That would be generally useful:

// print reciprocal of reduced range
range.reduce!someFunc.(n => 1 / n).writeln;

> The other problem is how much more verbose it is. With DIP 
> 1005, you can do
>
> with(import std.datetime)
> auto foo(SysTime st1, SysTime st2, Duration d);
>
> The import is only listed once, whereas with this technique, 
> you have to list it for each symbol.

That's a problem only when very few of a module's declarations 
need more than one symbol from the same module. How often does 
that occur when a module-level import should be avoided?


More information about the Digitalmars-d mailing list