I tend to use memoization heavily in my mostly-functional code. Memoize.jl 
is neat, but it doesn't use a WeakKeyDict. 

1. Is there any specific reason for that? Won't that code just hog the 
objects forever?

2. In SBCL, weak dictionaries and weak references had terrible O(N²) 
performance (I could never understand why), is that also the case for Julia?

3. In many cases, it makes more sense to store the memoized value inside 
the object's field (for instance, storing the hash for an image). That will 
be a good place to use Nullable, right? 

# Obviously I don't mean to recreate Image specifically
type Image
   ...
   hash_value::Nullable{int}
end

function hash(img::Image)
   if isnull(img.hash_value)
       return img.hash_value = ...
   else
       return get(img.hash_value)
   end
end

4. I still don't have the hang of Julia macros. In Common Lisp, I could 
easily write my own
(defstruct* Image
   ... normal fields
   (cache hash))
and have the above code automatically generated. What's the best syntax to 
use with Julia? Something like
@type_cached Image begin
   ...
   cache(hash)
end

function hash_impl(img::Imaeg)
   ...
end
? I don't like much how the `begin` makes it stick out, whereas Common 
Lisp's version is much more seamless. Isn't there a way to tell the parser 
"keep reading this macro until `end`" without having a begin statement?

Is there a better, more native solution to this problem?

Cédric

Reply via email to