assert(false)
monarch_dodra
monarchdodra at gmail.com
Fri Jun 21 01:51:48 PDT 2013
On Thursday, 20 June 2013 at 23:36:35 UTC, Joseph Rushton
Wakeling wrote:
> On 06/20/2013 11:36 PM, Jonathan M Davis wrote:
>>> ... one is necessary as it's in a function that is supposed
>>> to return a
>>> value.
>>
>> I don't think that that's supposed to be necessary. The only
>> reason that that should happen is if the compiler can
>> deterimine that the execution can definitely reach the end of
>> the function.
>
> It's in diceImpl:
>
> private size_t diceImpl(Rng, Range)(ref Rng rng, Range
> proportions)
> if (isForwardRange!Range && isNumeric!(ElementType!Range)
> && isForwardRange!Rng)
> {
> double sum = reduce!("(assert(b >= 0), a + b)")(0.0,
> proportions.save);
> enforce(sum > 0, "Proportions in a dice cannot sum to
> zero");
> immutable point = uniform(0.0, sum, rng);
> assert(point < sum);
> auto mass = 0.0;
>
> size_t i = 0;
> foreach (e; proportions)
> {
> mass += e;
> if (point < mass) return i;
> i++;
> }
> // this point should not be reached
> assert(false);
> }
>
> Obviously one can see logically that this assert will never be
> reached, but I
> don't think there's anything the compiler can pick up on to be
> certain.
Reading that code, it's not immediately obvious to me: The assert
makes it self documenting.
In any case, this goes back to what I first said: If you remove
that assert, the compiler will complain that not all paths return.
More information about the Digitalmars-d-learn
mailing list