What is the 'Result' type even for?

Salih Dincer salihdb at hotmail.com
Fri Jan 20 11:51:13 UTC 2023


On Friday, 20 January 2023 at 04:46:07 UTC, Ali Çehreli wrote:
> Different instantiations of templates are distinct types. For 
> example, if I called 'alternate' with two 'long' values, both 
> alternate!int (as instantiated by the code above) and 
> alternate!long would have different MyResult struct types.

In general, the ranges are compatible with each other because 
they use the empty, front, popFront interface. In the example 
below, different types (one of which is double) but the same 
ranges can be combined with chain(). However, it is necessary to 
convert it to array because of the opCmp() compatibility from 
algorithms such as sorting.

```d
   import std.algorithm : sort;
   import std.conv      : to;
   import std.range;
   import std.stdio;


   enum limit = 5;
   enum step = limit / 10.0;/*
   enum step = 1; //*/

void main()
{
   TypeInfo rangeType;	
	
   auto a = iota(limit);
   auto b = iota(step, limit, step);
	
   /* <- toggle comment, please add -> /
   auto ab = chain(a, b);
   rangeType = typeid(ab);/*/
   auto arrA = a.array.to!(double[]);
   auto arrB = b.array;
   auto ab = chain(arrA, arrB);
   rangeType = typeid(ab.sort);//*/
		
   ab.writeln(": ", rangeType);
} /*
current print:
==============
[0, 0.5, 1, 1, 1.5, 2, 2, 2.5, 3, 3, 3.5, 4, 4, 4.5]: 
std.range.SortedRange!(std.range.chain!(double[], 
double[]).chain(double[], double[]).Result, "a < b", 
0).SortedRange

other print:
============
[0, 1, 2, 3, 4, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]: 
std.range.chain!(std.range.iota!(int, int).iota(int, int).Result, 
std.range.iota!(double, int, double).iota(double, int, 
double).Result).chain(std.range.iota!(int, int).iota(int, 
int).Result, std.range.iota!(double, int, double).iota(double, 
int, double).Result).Result
```
SDB at 79


More information about the Digitalmars-d-learn mailing list