www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - ReturnType and overloaded functions

reply "Yuxuan Shui" <yshuiv7 gmail.com> writes:
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?
Jun 12 2015
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Jun 12 2015
next sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Fri, 12 Jun 2015 16:32:37 -0700, Ali =C3=87ehreli 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?
=20 I am curious about the answer myself but there is the workaround of passing the overload through a lambda: =20 import std.traits; =20 int foo(long) { return 0; } =20 short foo(byte) { return 0; } =20 void main() { static assert(is (ReturnType!(() =3D> foo(long.init)) =3D=3D int)); static assert(is (ReturnType!(() =3D> foo(byte.init)) =3D=3D short))=
;
 }
=20
 Ali
or without importing `std.traits`: static assert(is(typeof(foo(long.init)) =3D=3D int)); static assert(is(typeof(foo(byte.init)) =3D=3D short)); =
Jun 12 2015
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Jun 12 2015
parent ketmar <ketmar ketmar.no-ip.org> writes:
On Fri, 12 Jun 2015 17:28:56 -0700, Ali =C3=87ehreli wrote:

 void main()
 {
       static assert(is (ReturnType!(() =3D> foo(long.init)) =3D=3D int)=
);
       static assert(is (ReturnType!(() =3D> foo(byte.init)) =3D=3D shor=
t));
 }

 Ali
or without importing `std.traits`: static assert(is(typeof(foo(long.init)) =3D=3D int)); static assert(is(typeof(foo(byte.init)) =3D=3D short));
Good point. :) What is the difference of ReturnType then?
i don't know. it looks nicer, maybe. ;-)=
Jun 13 2015
prev sibling parent reply "Freddy" <Hexagonalstar64 gmail.com> writes:
 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
Why not just use templates? int foo(size_t id)() if(id == 0){ return 0; } short foo(size_t id)() if(id == 1){ return 0; } static assert(is(typeof(foo!0()) == int)); static assert(is(typeof(foo!1()) == short));
Jun 12 2015
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/12/2015 06:44 PM, Freddy wrote:

 Why not just use templates?
The question is about overloaded functions. Ali
Jun 12 2015
prev sibling next sibling parent reply "Idan Arye" <GenericNPC gmail.com> writes:
On Friday, 12 June 2015 at 23:26:00 UTC, 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?
The return type of the first declared one: http://dpaste.dzfl.pl/f448ec624592
Jun 12 2015
parent reply Kenji Hara via Digitalmars-d <digitalmars-d puremagic.com> writes:
2015-06-13 9:29 GMT+09:00 Idan Arye via Digitalmars-d <
digitalmars-d puremagic.com>:

 On Friday, 12 June 2015 at 23:26:00 UTC, 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?
The return type of the first declared one: http://dpaste.dzfl.pl/f448ec624592
That's definitely a bug in current dmd. ReturnType should make error when overloaded function symbol is given. To pick up one of the overloaded functions, you can use __traits(getOverloads). Kenji Hara
Jun 12 2015
parent "Yuxuan Shui" <yshuiv7 gmail.com> writes:
On Saturday, 13 June 2015 at 05:14:00 UTC, Kenji Hara wrote:
 2015-06-13 9:29 GMT+09:00 Idan Arye via Digitalmars-d <
 digitalmars-d puremagic.com>:

 On Friday, 12 June 2015 at 23:26:00 UTC, 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?
The return type of the first declared one: http://dpaste.dzfl.pl/f448ec624592
That's definitely a bug in current dmd. ReturnType should make error when overloaded function symbol is given. To pick up one of the overloaded functions, you can use __traits(getOverloads). Kenji Hara
It's kind of pointless to pick any one of the overloads. It would be convenient if a template exists to help us pick a overload by matching its parameter type. That being said, the solution given earlier in this thread seems to work, I just wonder if there're any caveats.
Jun 13 2015
prev sibling parent Artur Skawina via Digitalmars-d <digitalmars-d puremagic.com> writes:
On 06/13/15 01:25, Yuxuan Shui via Digitalmars-d 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?
alias ReturnType(alias F, A...) = typeof(F(A.init)); artur
Jun 13 2015