memoize
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Tue Jan 4 06:15:16 PST 2011
On 1/4/11 7:59 AM, Simen kjaeraas wrote:
> Guilherme Vieira <n2.nitrogen at gmail.com> wrote:
>
>> @Walter: would it be hard/impossible for the compiler to look at
>> memoize and
>> tell it exhibits pure behavior and is, thus, pure?
>
> The simplest solution is this:
>
> template memoize( alias fn ) {
> static if ( isPure!fn ) {
> pure auto memoize( ParameterTypeTuple!fn ) {
> // Blah!
> }
> } else {
> auto memoize( ParameterTypeTuple!fn ) {
> // Blah!
> }
>
> }
> }
I've been mulling over this for a while, maybe it's time to start
discussing it.
Often you want to say "this entity is pure/const/immutable/safe... if
this other entity is the same, or generally if this Boolean is true". So
I was thinking of introducing e.g. a constrained pure:
template memoize( alias fn ) {
pure(isPure!fn) auto memoize( ParameterTypeTuple!fn ) {
...
}
}
So generally when you write "attribute(expression)" the attribute will
be in effect if and only if the expression is true.
D has become very powerful at introspecting most of its own abstractions
in the form of compile-time Booleans. This language extension would
close the circle by allowing D to introduce attributes and qualifiers
depending on compile-time Booleans.
Andrei
More information about the Digitalmars-d
mailing list