trusted purity?
monarch_dodra
monarchdodra at gmail.com
Mon Aug 19 02:55:50 PDT 2013
I've been struggling with this, so here are my observations:
On Monday, 29 April 2013 at 18:31:15 UTC, Walter Bright wrote:
> On 4/29/2013 3:58 AM, monarch_dodra wrote:
>> Is there *any* way to make a call to a non-pure function in a
>> pure context, if
>> you know you won't violate your own purity?
> 2. put the impure code in a separate function, take its
> address, and cast its address to being a pointer to a pure
> function. (Of course, such a cast should be rejected by @safe
> code.)
This doesn't work with CTFE. I'm currently not seeing how I could
make a function that needs to make a "trusted pure" call work at
compile time: The function pointer cast will fail during CTFE,
and if I add a "if (__ctfe)" block without it, then the function
will be impure, due to the code inside the "if (__ctfe)" block.
I've yet to solve this problem.
> 3. Put the code in an extern(C) function, compiled separately
> as impure, but declared as pure in the client. C functions
> don't get name mangling, so the compiler won't know it's impure.
Unfortunately, this doesn't work with templates. You have to
force instantiation by inserting a straight up (dummy) call to
the function, but that immediately makes the caller impure...
> I feel it's a good thing that you'll need to jump through some
> hoops to do this, otherwise 'pure' would not be very useful.
I agree, but these aren't hoops, they're pole vaults.
FYI, the problem I'm trying to fix is this one:
* "uninitializedArray" returns an array with un-initialized
elements. This, by definition, is not pure, since the value
returned is garbage. I'm fixing the function so that it becomes
*impure*.
* "array" is implemented in terms of "uninitializedArray":
Allocate an array, and then fill it. "array" is pure, since its
return is defined. array also works with ctfe.
I'm at a deadlock on this one.
More information about the Digitalmars-d
mailing list