AliasSeq seems to compile slightly faster with static foreach

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Jan 8 21:09:54 UTC 2018


On Monday, January 08, 2018 10:40:41 Ali Çehreli via Digitalmars-d wrote:
> On 01/07/2018 06:20 PM, Jonathan M Davis wrote:
>  > I misremembered how iota worked with a single
>  > argument; I thought that iota(0) created an infinite range, whereas it
>  > creates a zero-length range.
>
> I told you so! :)
>
> I remember pointing out that specific inconsistency of iota, I think in
> the learn forum but the simplicity of n.iota was too attractive for
> others to agree with me. (I think it was that mythical character,
> bearophile, who did not agree with me but I can't find that newsgroup
> discussion now.)

I can see arguments both for having the single argument be the first element
and for it being the one before the last, but the ambiguity makes it easy
enough to screw it up either way if you're not thinking the same way as
whoever picked that behavior, and you either assume which it is or
misremember.

I suppose that in practice, there really isn't much difference between
iota(0) and iota(int.max). The range of values isn't actually infinite, so
iota(0) is either going to have to be an infinte range that wraps around
when it hits int.max, or it would just be identical to what iota(int.max) is
now (with some logical trickery, iota(0) could be made to also give the
value int.max, whereas iota(int.max) couldn't, but that would complicate
iota a bit). The main advantage to iota(size_t.max) over iota(0) is that it
forces size_t without a cast, whereas iota(0) will just infer as int. Either
way, by only have one argument, the behavior is non-obvious, and if I
remember it correctly now, it will mostly be because I screwed up so badly
with it. Because the behavior is not obvious, I suspect that plenty of folks
have to look it up (especially if they don't use it often). And clearly, I
should have looked it up. We probably would have been better off not
allowing a single argument version, but what's done is done, I guess. At
least, I'm a lot less likely to misremember how it works now.

- Jonathan M Davis




More information about the Digitalmars-d mailing list