ReturnType and overloaded functions

Ali Çehreli via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 12 17:28:56 PDT 2015


On 06/12/2015 05:04 PM, ketmar wrote:
> On Fri, 12 Jun 2015 16:32:37 -0700, Ali Çehreli wrote:
>
>> On 06/12/2015 04:25 PM, Yuxuan Shui wrote:
>>> When there are multiple overloaded functions, whose return type will I
>>> get when I use ReturnType? Is there a way I could choose a specific
>>> function by its parameter types?
>>
>> I am curious about the answer myself but there is the workaround of
>> passing the overload through a lambda:
>>
>> import std.traits;
>>
>> int foo(long)
>> {
>>       return 0;
>> }
>>
>> short foo(byte)
>> {
>>       return 0;
>> }
>>
>> void main()
>> {
>>       static assert(is (ReturnType!(() => foo(long.init)) == int));
>>       static assert(is (ReturnType!(() => foo(byte.init)) == short));
>> }
>>
>> Ali
>
> or without importing `std.traits`:
>
>    static assert(is(typeof(foo(long.init)) == int));
>    static assert(is(typeof(foo(byte.init)) == short));
>

Good point. :) What is the difference of ReturnType then?

Ali



More information about the Digitalmars-d mailing list