If you have e.g. a function that always returns Int, then you can specify 
the associative type as Dict{Any,Int}, which allows type inference to 
determine that anything pulled out of it is an Int, so the return type of 
the memoized function can be inferred:

julia> using Memoize

julia> @memoize Dict{Any,Int} f(y) = y + 1

julia> Base.return_types(f, (Int,))
1-element Array{Any,1}:
 Int64

Of course this doesn't work if you don't specify the Dict type:

julia> @memoize f(y) = y + 1

julia> Base.return_types(f, (Int,))
1-element Array{Any,1}:
 Any

Simon

On Wednesday, March 5, 2014 2:53:09 PM UTC-5, andrew cooke wrote:
>
> we're talking about  
> https://github.com/simonster/Memoize.jl/blob/master/src/Memoize.jl ?
>
> i must be missing something.  where is the type?
>
> thanks,
> andrew
>
> On Wednesday, 5 March 2014 16:24:34 UTC-3, Pierre-Yves Gérardy wrote:
>>
>> It already accepts a type as an optional parameter.
>>
>> On Wednesday, March 5, 2014 7:41:42 PM UTC+1, andrew cooke wrote:
>>>
>>>
>>> it seemed to me (when i looked at the code) that would be trivial to add 
>>> to Memoize.jl by adding a type parameter to the macro and reading from the 
>>> cache into a typed variable.
>>>
>>> but since i spilt coke all over my netbook yesterday my julia 
>>> programming is on hiatus :o(
>>>
>>> andrew
>>>
>>>
>>> On Wednesday, 5 March 2014 10:26:37 UTC-3, Toivo Henningsson wrote:
>>>>
>>>> The trouble with memoization and type inference is that when type 
>>>> inference sees a memoized function, it will see that the return value is 
>>>> either computed, or looked up in the cache. The type of the cached value 
>>>> might be anything, unless the user supplies an explicit type restriction. 
>>>> There's no way to tell type inference that the cached must have been 
>>>> computed by the function previously, so that it can be disregarded when it 
>>>> comes to inferring the return type.
>>>>
>>>> On Wednesday, 5 March 2014 01:34:10 UTC+1, Pierre-Yves Gérardy wrote:
>>>>>
>>>>>
>>>>>
>>>>> On Tuesday, March 4, 2014 3:01:13 AM UTC+1, andrew cooke wrote:
>>>>>>
>>>>>>
>>>>>> i assumed that passing by value was an optimization, but i don't know 
>>>>>> what triggers it.  it may be that it's only for immutable types without 
>>>>>> pointers to mutable types.  or it may be based on size.  it might be 
>>>>>> something to worry about if your instances are large.
>>>>>>
>>>>>> there's a memoize.jl module somewhere that defines a macro that 
>>>>>> automatically memoizes the function it's applied to.  so you could use 
>>>>>> that 
>>>>>> on your constructor function, but:
>>>>>>
>>>>>>  1 - i'm not sure how nesting affects things
>>>>>>
>>>>>  2 - when i looked at memoize.jl, by default it used a simple map (no 
>>>>>> weak keys or lru eviction)
>>>>>>
>>>>>
>>>>> Yes, but you can pass a collection of your choice as a cache. Julia 
>>>>> provides a WeakKeyDict, but if your function depends on multiple inputs, 
>>>>> using tuples as keys makes it moot.
>>>>>
>>>>> I'll go ahead and skip this whole identity thing for now :-)
>>>>>
>>>>>  3 - someone said that using memoize.jl broke type inference of return 
>>>>>> arguments (i don't understand why) which had a performance impact.
>>>>>>
>>>>>
>>>>> That was Stephan Karpinski. I wonder if it holds when using a 
>>>>> `memoize` macro that creates a named function and a typed cache.
>>>>>  
>>>>> —Pierre-Yves
>>>>>
>>>>

Reply via email to