trusted purity?

Walter Bright newshound2 at digitalmars.com
Mon Apr 29 11:31:15 PDT 2013


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?

Vee haf veys:

1. put "debug" before the impure code (but you'll have to compile with -debug)

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.)

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.


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.



> Related question:
> Can a function that "sometimes throws" be considered as pure?

deadalnix's answer is correct.



More information about the Digitalmars-d mailing list