Purity (D2 standard libraries / object.d)

Stewart Gordon smjg_1998 at yahoo.com
Sat Jan 10 12:28:01 PST 2009


Walter Bright wrote:
<snip>
> Memoization and purity don't mix. For each function, you'll have to pick 
> which way you want to go. I don't see what that has to do with D's 
> future, though, the language allows either.

Actually, what they don't mix with is the requirement that it still be 
callable on a mutable object.

There's
http://d.puremagic.com/issues/show_bug.cgi?id=1824
and, while it's a nice idea, it precludes caching of the result.  Unless 
you want to keep the cache separate from the object.

A pure toString may be a nice idea; however, this precludes using any 
mutable members of the class to generate it.  But it would also be nice 
to be able to call such methods on all objects of the class, mutable or not.

One possibility is to have separate functions

     string toString()
     string toString() invariant

and let the programmer implement memo(r)ization on the mutable version 
if desired, whereas the invariant version would have it generated by the 
compiler.  But there are still problems
- nobody would know which to call if the reference is const, rather than 
mutable or invariant
- it would be necessary to maintain two versions of toString in parallel

Maybe there's a solution somewhere out there....

Stewart.



More information about the Digitalmars-d mailing list