www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - getOverloads order

reply IchorDev <zxinsworld gmail.com> writes:
I've noticed that `__traits(getOverloads)` always returns the 
overloads in lexical order across DMD, LDC, and GDC. Is this 
reliable at all?
Jul 13 2023
parent reply Dennis <dkorpel gmail.com> writes:
On Thursday, 13 July 2023 at 08:03:02 UTC, IchorDev wrote:
 I've noticed that `__traits(getOverloads)` always returns the 
 overloads in lexical order across DMD, LDC, and GDC. Is this 
 reliable at all?
No. It depends on the order the compiler analyzes the symbols, which is often lexical order, but it can vary based on static if, mixin, forward references etc. Here's a counter example: ```D void f(int x); mixin("void f(float y);"); void f(char z); ``` Here you get overloads of `f` in the order (x, z, y) instead of (x, y, z).
Jul 13 2023
parent reply IchorDev <zxinsworld gmail.com> writes:
On Thursday, 13 July 2023 at 10:53:49 UTC, Dennis wrote:
 On Thursday, 13 July 2023 at 08:03:02 UTC, IchorDev wrote:
 I've noticed that `__traits(getOverloads)` always returns the 
 overloads in lexical order across DMD, LDC, and GDC. Is this 
 reliable at all?
No. It depends on the order the compiler analyzes the symbols, which is often lexical order, but it can vary based on static if, mixin, forward references etc. Here's a counter example: ```D void f(int x); mixin("void f(float y);"); void f(char z); ``` Here you get overloads of `f` in the order (x, z, y) instead of (x, y, z).
Well that makes sense, but also wouldn't apply to the use-case that I was considering, since all the code would be in one mixin. However, the spec doesn't specify that this is how `getOverloads` **must** work; is this guaranteed behaviour but the spec simply omits it?
Jul 13 2023
parent Dennis <dkorpel gmail.com> writes:
On Thursday, 13 July 2023 at 11:04:40 UTC, IchorDev wrote:
 However, the spec doesn't specify that this is how 
 `getOverloads` **must** work; is this guaranteed behaviour but 
 the spec simply omits it?
The order is not guaranteed. I don't know why you need a specific order, but perhaps you can sort based on `__traits(getLocation)`.
Jul 13 2023