Creating a custom iota()
H. S. Teoh
hsteoh at quickfur.ath.cx
Thu May 12 16:57:35 UTC 2022
On Thu, May 12, 2022 at 11:57:54AM +0000, realhet via Digitalmars-d-learn wrote:
[...]
> I have my own DateTime struct.
> It has opCmp() and opBinary(), I can do arithmetic with this custom
> DateTime and the amazing time units of the **quantities** package.
>
> Now I'm about mo make iterations in a DateTime range:
>
> const
> st = DateTime(UTC, "22.1.1 8:30").utcDayStart,
> en = DateTime(UTC, "22.3.5 19:56").max(now).utcDayStart +
> (100.0/64)*nano(second);
>
> //this works
> for(auto d = cast()st; d < en; d += day) writeln(d);
>
> //this would be nicer, but not works
> iota(st, en, day).each!writeln;
>
> My question is, is there a way to 'extend' the functionality of the
> std.iota() function or it is better to come up with a unique name for
> this special 'iota' functionality?
Custom user types should already be supported by iota; see:
https://issues.dlang.org/show_bug.cgi?id=10762
Does your DateTime type support the `++` operator? If not, that's the
most likely reason iota failed to instantiate on it. Just add
`opUnary(string op : "++")` to your type, and it should work.
[...]
> Or maybe put it inside a DateTimeRange struct, and implement the std
> range functionality?
[...]
That would work too.
But I'm curious, why didn't you use std.datetime? The DateTime type
there supports inter-date arithmetic (as far as it makes sense), and can
be easily made into a range using std.range.recurrence.
T
--
When solving a problem, take care that you do not become part of the problem.
More information about the Digitalmars-d-learn
mailing list