Reputation: 12138
After having read this interesting article about Ada and C++ and knowing of D's support for CTFE and constant-parameter specialization of functions I wonder if Ada-Style Range types could be more easily/efficiently implemented in D than in C++. Has anybody perhaps already written such a library?
If such ranges could be implemented efficiently and developer-friendly in D it could be used as a promotor for establishing D in sectors with demands on determinism and type- and memory-safety (were D already shines) such as in avionics and automotive. D would thereby gain more developer-interest and stronger financial support.
Upvotes: 3
Views: 224
Reputation: 19787
Having scalar (bounded) variable is easily done in D as a template, and in fact I remember I saw the code that someone already did it. Unfortunately I do not remember where I saw it. This said, there is IMHO no need for this to become part of the language, but rather part of the standard library.
(Edit: Adam reminded me of the code: http://arsdnet.net/dcode/ranged.d )
Ranges are more wider concept nicely explained in Andrei's article - http://www.informit.com/articles/printerfriendly.aspx?p=1407357&rll=1 . This type of ranges are now a core concept of D. D's slice is an implementation of the most powerful range - RandomAccessRange.
Example:
import std.stdio;
import std.algorithm;
void main()
{
int[] values = [ 1, 20, 7, 11 ]; // values is a RandomAcessRange
writeln(filter!(value => value > 10)(values));
}
Good reads:
Upvotes: 4
Reputation: 25595
I wrote some little code that does min and max of integers with overflow check:
http://arsdnet.net/dcode/ranged.d
This was just a proof of concept though, I doubt it will perform very well, but might if inlined.
Upvotes: 3