assert(false)

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Thu Jun 20 16:36:13 PDT 2013


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.

Incidentally, I just realized that with diceImpl, Phobos has a very nice
function in place to support a simple (though possibly inefficient)
implementation of the Barabasi-Albert algorithm for generating scale free
networks ... :-)


More information about the Digitalmars-d-learn mailing list