Mixed int/BigInt foreach interval

Jonathan M Davis jmdavisProg at gmx.com
Mon Sep 26 19:06:12 PDT 2011


On Monday, September 26, 2011 21:49:49 bearophile wrote:
> Jonathan M Davis:
> > Why is it bad? isIntegral was specifically designed for testing that a
> > type was one of byte, ubyte, short, ushort, int, uint, long, and ulong,
> > and that's exactly what it's doing.
> 
> Going back to my original post, is changing the semantics of isIntegral
> enough to allow code like this to compile and run?
> 
> foreach (i; 1 .. BigInt(10)) {}

No. That's foreach. BigInt isn't going to work with the .. syntax regardless 
of what isIntegral does. I assume what you're really asking is whether

foreach(i; iota(1, BigInt(10))) {}

would work. And the answer is definitely no. iota is too complex for simply 
changing isIntegral to work. For instance, CommonType would fail, because 
BigInt and int are not implicitly convertible between one another. I'm sure 
that iota could be reworked such that it could work with BigInt and work with 
a combination of int and BigInt, but it's definitely not as simple as simply 
messing around with the template constraint.

- Jonathan M Davis


More information about the Digitalmars-d mailing list