trusted purity?
Jonathan M Davis
jmdavisProg at gmx.com
Mon Apr 29 11:47:53 PDT 2013
On Monday, April 29, 2013 12:58:44 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?
>
> This is something you can do with @safe (@trusted), but what
> about pure?
>
> For example, "free" is not pure, because you can't call it twice
> on the same pointer. But if you manage the pointer yourself
> inside a struct, you can guarantee the purity of your own
> functions. But the language won't allow you to do that.
You can cast a pointer to the function. std.datetime does that for the
LocalTime and UTC singletons. Take a look at the semi-recently added
std.traits.SetFunctionAttributes.
> Related question:
> Can a function that "sometimes throws" be considered as pure?
Throwing has nothing to do with purity. pure is purely a question of whether
the function accesses module-level or static variables which can possibly be
mutated after they're initialized. Strong purity (which is required for
most/all optimizations) is then places additional requirements on the function
parameters, but purity itself is simply a question of whether the function
accesses module-level or static variables. So, throwing has nothing to do with
it.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list