isInfinite isInadequate
monarch_dodra
monarchdodra at gmail.com
Tue Mar 12 03:49:56 PDT 2013
On Tuesday, 12 March 2013 at 10:01:38 UTC, deadalnix wrote:
> I want to resurrect that thread. Can someone explains the
> benefices of isInfinite ? I fail to see how it really benefit
> the code.
The advantage of "enum empty = false" is that algorithms gain a
great performance boost by optimizing out any "if (r.empty)".
This can be exploited for things like take, or anything that
iterates as a matter of fact. I don't think anybody will argue
that this is a bad approach.
The trait itself checks if empty can be evaluated at compile time
(to false). The advantage for the *coder* to know if the range is
infinite is less obvious.
One of the advantages is that an infinite range can have random
access (meets RA requirements), even though it has no length
member (normally, any RA range must have length).
Having "isInfinite" can also have the advantage of protecting
users from stupid calls. For example, calling "count" on an
infinite range is forbidden => shifting problems from runtime to
compile time is a HUGE gain.
One of downsides to having infinite ranges is that their
existence tends to make dealing with generic RA ranges a bit more
difficult. A lot of our algorithms have changed requirements
conditions from:
"if (isRandomAccessRange!R)"
to
"if (isRandomAccessRange!R && hasLength!R)"
or
"if (isRandomAccessRange!R && !isInfinite!R)"
//NOTE: Both are strictly equivalent: An RA range is either
infinite, or has length, but not neither nor both.
Up until not so long ago, it was not possible to slice infinite
ranges. It is now possible. Unfortunatly, because RA ranges with
slicing don't guarantee that r[0 .. $] is legal, things are still
complicated in template code.
The last point (IMO minor) that is raised in the thread is
"runtime" infiniteness. EG: a range that may or may not be
infinite at compile time, but which will be known at runtime.
IMO, these a rare enough (arguably, inexistent) to not really
matter. The workarounds are easy:
1) Make the range always infinite, but with a requirement that
user must intialize it or whatnot.
2) Make a runtime fork that will call code that operates on a
compile-time known infinite adaptor/subtype.
More information about the Digitalmars-d
mailing list