Poll: a nonstate keyword

Janice Caron caron800 at googlemail.com
Sat May 31 02:37:44 PDT 2008


On 31/05/2008, Fawzi Mohamed <fmohamed at mac.com> wrote:
>  Sorry , basically I want a hack to convince the compiler that even if a
> function looks not pure it actually is.

The one example of this which I can recall, which has been
demonstrated in this forum, is calling a "helper function" from within
a pure function. Is that what you're talking about? If so, it's been
mentioned before. Here's an example:

    void helper(char[] s) // not pure
    {
        s[0] = 'J';
    }

    string foo(string s) pure
    {
        char[] tmp = s.dup;
        helper(tmp);
        return assumeUnique(tmp);
    }

Currently, it seems that the above might not be legal code, since pure
functions cannot call non-pure functions. However, what makes this
case (and others like it) special is that, although helper itself is
not pure, if it were inlined, it wouldn't stop foo from being pure.

This has been pointed out before, and I am /sure/ that Walter will
come up with a solution which makes it possible (though not
necessarily in the first attempt).

I have absolutely no idea, however, what this has to do with Steven's
question. Is it relevant, or are we heading off-topic?



>  Maybe a cast, like this (or with an uglier name)
>  pure int function(T) x=cast(pure int function(T))&y;

The way I read it, the following will work, as is already legal syntax

    auto x = cast(function int(T) pure)&y;


> > One of the advantages of pure functions, however, is that (like
> > inlining) memoization can be done /by the compiler/, so you don't have
> > to do it yourself. How good a job the compiler will do remains to be
> > seen. My guess would be, poor at first, getting increasingly better
> > over time.
>
>  Sorry that is not good enough for me I want functions lifted out of inner
> loops, and a compiler that says to me either you use the slow pure function,
> and I will lift it out of the loop, or the fast one, but it will stay in the
> loop, is not good enough for me.
>  I want a hole to be able to trick the compiler into floating out the fast
> function.

I still don't get it. Can you show me what you mean with an example?



More information about the Digitalmars-d mailing list