static array is no range?

Sebastiaan Koppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 5 18:50:54 PDT 2015


On Sunday, 6 September 2015 at 00:25:10 UTC, cym13 wrote:
> Yes, static arrays aren't ranges. The main reason is that 
> static arrays are value type (ie: you copy them arround when 
> passing them to functions which usually has a huge cost) where 
> ranges are reference type (no copy, lighter, not always better 
> as it makes optimisation more complicated).
>
> The standard library is designed arround ranges to make sure 
> that you are not copying 1024-bytes long structures arround by 
> accident: you'd have to do that explicitely. As a consequence, 
> you must generally slice arrays when passing them to phobos 
> functions (not always true but a good rule of thumb).

That actually makes a lot of sense.

> That said, if you want to benefit from array-specific 
> optimisations such as loop-unrolling you are generally better 
> of using a good old foreach and implementing the logic 
> yourself. Yes, it is sad, I agree.

While I like speed, 95% of my applications can be 4x as slow, and 
no-one would give a damn. Plus, I have only limited time so I try 
to be efficient by writing as little code as possible (while 
still getting work done.)

What I was trying to say is that endorsed idiomatic D code (UFCS 
combined with function overloading+constraints), produces 
terrible error messages. Which means every newcomer sees that 
awful stuff and decides to implement said algorithm himself. That 
is not productivity.

This is arguably the poorest and most neglected aspect of D.

Without messing up internals the best I can think of is this:

```
auto forwardRange(alias R)()
{
	import std.traits;
	import std.range;
	static if(!isForwardRange!(typeof(R))) static 
assert(0,"Nonono... "~__traits(identifier,R)~" not a 
ForwardRange");
	return R;
}

unittest
{
	char[1024] buffer;
	import std.algorithm;
	forwardRange!buffer.find("LOCATION: ");
}
```

Which is ugly as hell, and probably even worse than the current 
state.


More information about the Digitalmars-d-learn mailing list