memoize (is this a fix for overloading?)

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Wed Jan 5 04:12:39 PST 2011


On Wed, 05 Jan 2011 13:40:05 +0200, Max Samukha wrote:

> On 01/05/2011 12:24 PM, Lars T. Kyllingstad wrote:
>> On Wed, 05 Jan 2011 12:07:50 +0200, Max Samukha wrote:
>>
>>> On 01/05/2011 11:21 AM, Lars T. Kyllingstad wrote:
>>>> On Tue, 04 Jan 2011 17:06:45 -0600, Andrei Alexandrescu wrote:
>>>>> [...]
>>>>> Overloads are compile-time entities so they should be easily
>>>>> inspectable.
>>>>
>>>> Since this is likely to be useful in other cases as well, maybe it
>>>> would be better to make a general template that selects an overload?
>>>> So you'd use it like this:
>>>>
>>>>     alias memoize!(selectOverload!(sqrt, double)) msqrt;
>>>>
>>>> A first step towards this would be to make __traits(getOverloads)
>>>> work with module-level functions.  Currently it only works for member
>>>> functions.  Also it would probably be better if it accepted a
>>>> function alias instead of a string containing the function name.
>>>
>>> __traits(getOverloads) does work with module level functions if you
>>> pass a module alias to it.
>>
>> Cool!  I didn't know that.  Here's an updated version, where
>> selectOverload works with both module-level and member functions:
>>
>> [...]
> 
> Nice! Though I think that overloads should be selected not by type
> equality but by regular overload resolution rules. That is the template
> should take not parameter but argument types.

I agree.  It is actually trivial to modify my example by replacing 
equalTuple!() with implicitlyConvertibleTuple!() which uses an is(T1:T2) 
test instead of is(T1==T2).  But that would make the template select the 
first possible match, instead of the "best match" resolution used by the 
compiler, which is often not what you want.  It would fail miserably for 
the sqrt() example, for instance, by always picking out sqrt(float).

In the end, I think implementing full overload resolution in the library 
will be so hairy it will be better to just add __traits(selectOverload).  
This doesn't look too bad:

  alias memoize!(__traits(selectOverload, sqrt, double)) msqrt;

And if, one day, __traits gets replaced by the meta namespace (bug 3702, 
one of the highest-voted in Bugzilla), it actually looks pretty good:

  alias memoize!(meta.selectOverload(sqrt, double)) msqrt;

;)
-Lars


More information about the Digitalmars-d mailing list