digitalmars.D.learn - Type name shadowing
- ixid (11/11) Oct 25 2014 T shadow(T = int)(T a) {
- H. S. Teoh via Digitalmars-d-learn (20/35) Oct 25 2014 The problem gets worse than that. For example:
T shadow(T = int)(T a) { alias T = string; T b = "hi"; T c = 1; // Error writeln(typeof(a).stringof); // int writeln(typeof(b).stringof); // string return a; } Are there uses for this shadowing of type names? It seems a little dangerous, for example ulong T could be shadowed by uint T. Is there a reason to allow it?
Oct 25 2014
On Sat, Oct 25, 2014 at 12:28:39PM +0000, ixid via Digitalmars-d-learn wrote:T shadow(T = int)(T a) { alias T = string; T b = "hi"; T c = 1; // Error writeln(typeof(a).stringof); // int writeln(typeof(b).stringof); // string return a; } Are there uses for this shadowing of type names? It seems a little dangerous, for example ulong T could be shadowed by uint T. Is there a reason to allow it?The problem gets worse than that. For example: ----external_library.d---- module external_library; alias T = string; ----main.d---- module main; void func(T = int)(T i) { import external_library; pragma(msg, T.stringof); // prints 'string' } void main() { func(1); } Imagine that the 'alias T' was not present in an earlier version of the library, but now has been added by the library author. Suddenly, user code breaks without warning. T -- Error: Keyboard not attached. Press F1 to continue. -- Yoon Ha Lee, CONLANG
Oct 25 2014