Pregunta

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.

¿Fue útil?

Solución 2

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.

Otros consejos

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:

  1. http://ddili.org/ders/d.en/ranges.html
  2. http://www.drdobbs.com/architecture-and-design/component-programming-in-d/240008321
  3. http://dlang.org/phobos/std_range.html
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top