For some inspiration on method caching, you might take a look at the 
`broadcast!` 
code: https://github.com/JuliaLang/julia/blob/master/base/broadcast.jl#L219.

The body of the `broadcast!` function essentially asks, "Have I been asked 
to broadcast this function before? If so, have I been asked to broadcast it 
over this many arrays before? If so, grab the locally defined function that 
does the broadcasting! If not, generate that function, store it in the 
cache, and give it to me now so I can call it on the original arguments." 
This actually uses a doubly nested caching system, which you can see in the 
three dicts at work (`cache`, `cache_f`, `cache_f_na`). 

Note also that there is some kinda tricksy metaprogramming going on, for 
instance with the use of `@get!` (which is not exported and whose 
definition can be found 
here: https://github.com/JuliaLang/julia/blob/master/base/dict.jl#L670-L687).

On Saturday, June 20, 2015 at 5:40:35 PM UTC-4, Laurent Bartholdi wrote:
>
> Dear Julia-list:
>
> I'm a quite heavy user of the computer algebra system GAP (
> www.gap-system.org), and wondered how some of its features could be 
> mapped to Julia.
>
> GAP is an object-oriented language, in which objects can acquire 
> attributes over time; and these attributes can be used by the method 
> selection to determine the most efficient method to be applied. For a 
> contrived example, integers could have a method "is_square" which returns 
> true if the argument is a perfect square. When the method is_square is 
> called, the result is stored in a cache (attached to the object). On the 
> second call to the same method, the value is just looked up in the cache. 
> Furthermore, the method "sqrt" for integers may test whether a cached value 
> is known for the method is_square, and look it up, and in case the argument 
> is a perfect square it may switch to a better square root algorithm. Even 
> better, the method dispatcher can do this behind the scenes, by calling 
> itself the square root algorithm for perfect squares in case the argument 
> is already known to be a square.
>
> (This is not really a GAP example; in GAP, only composed types (records, 
> lists) are allowed to store attributes. In the internals, each object has a 
> type and a bitlist storing its known attributes.)
>
> I read through the Julia manual, but did not see any features ressembling 
> those (caching results of methods, and allowing finer dispatching). I have 
> also read http://arxiv.org/pdf/1209.5145.pdf which says that "the type of 
> a value cannot change over its lifetime".
>
> Is there still a way to do similar things in Julia?
>
> Thanks in advance, and apologies if I missed this in the manual,
> Laurent
>

Reply via email to