digitalmars.D - Function overloading concern
- Sean Kelly (55/55) Mar 09 2006 While I appreciate the simple overloading rules in D, the recent
- Sean Kelly (10/10) Mar 09 2006 I mulled this over a bit more, and I think the existing rules will work
- Carlos Santander (20/93) Mar 09 2006 Does this work?
- Sean Kelly (3/11) Mar 09 2006 It does. alias is just too darn flexible for its own good :-)
- Sean Kelly (25/36) Mar 09 2006 By the way, the more complex C++ examples don't work in D anyway,
- Carlos Santander (11/45) Mar 09 2006 This one, I don't understand. I don't have DMD to try the code, but I th...
- Sean Kelly (12/59) Mar 09 2006 The above example doesn't really make sense in D but it's quite common
- Walter Bright (5/8) Mar 09 2006 I've seen that scheme. It strikes me as excessively complicated.
- Mike Capp (15/23) Mar 10 2006 The proposal is actually pretty good about backward compatibility. The
- Sean Kelly (5/13) Mar 10 2006 This is my concern as well. Just how far will they truly be able to
- Walter Bright (19/42) Mar 10 2006 I have the greatest admiration and respect for Daveed. But nobody is sma...
- Mike Capp (9/13) Mar 10 2006 I think that statement is a little extreme. Daveed is arguably just tryi...
- Bruno Medeiros (9/58) Mar 11 2006 Responding to both what Walter and Mike said:
- Walter Bright (3/3) Mar 09 2006 D doesn't allow overloading of ordinary functions with template function...
- Sean Kelly (4/7) Mar 09 2006 Yup, and I agree that this is a good thing. My example used normal
- Walter Bright (3/5) Mar 09 2006 That'll get fixed.
- Bob W (3/25) Mar 10 2006 I doubt that this would work in any programming language ;-)
- Derek Parnell (10/22) Mar 09 2006 However, this is exactly one of the reasons that alias was invented by
- Walter Bright (5/15) Mar 09 2006 Actually, alias was specifically designed so that will work. It enables ...
While I appreciate the simple overloading rules in D, the recent template improvements have me wondering how to solve cases where fully qualifying names is impossible. Here's an example using plain old functions: Module A: module a; void swap( inout int i1, inout int i2 ) { int t = i1; i1 = i2; i2 = t; } Module B: module b; struct S {} void swap( inout S s1, inout S s2 ) { S t = s1; s1 = s2; s2 = t; } Main: import a; import b; template func( T ) { void func( inout T t1, inout T t2 ) { swap( t1, t2 ); } } void main() { int i = 1, j = 2; func( i, j ); } C:\code\d>dmd test a b a.d(3): function a.swap conflicts with b.swap at b.d(5) test.d(15): template instance test.func!(int) error instantiating While I haven't run up against this situation yet, I can see it being an obstacle for large projects. How would I resolve something like this? And I expect the overloading rules will be the same for template functions as they are for normal functions? Finally, is there a more D-oriented approach that might address this without language changes? I suppose one language change that might help would be to support the externally defined method syntax for primitives, so func could be rewritten as: template func( T ) { void func( inout T t1, inout T t2 ) { t1.swap( t2 ); } } and the specialized swap for struct S could be placed within its own scope. However, this still doesn't address all situations, and I'm not entirely certain that it wouldn't introduce new overloading problems where none existed before. Sean
Mar 09 2006
I mulled this over a bit more, and I think the existing rules will work so long as template code is carefully designed with it in mind. For example, where a template might rely on another overloaded template in C++, it might be prudent to parameterize it in D. And with classes as reference types in D, there's a much smaller need for specialized swap functions. My only remaining concern is traits templates in D, as they basically rely on cross-module overloading. But perhaps this is something best sorted out by experimentation--DTL will likely have to deal with it sooner or later in any case ;-) Sean
Mar 09 2006
Sean Kelly escribió:While I appreciate the simple overloading rules in D, the recent template improvements have me wondering how to solve cases where fully qualifying names is impossible. Here's an example using plain old functions: Module A: module a; void swap( inout int i1, inout int i2 ) { int t = i1; i1 = i2; i2 = t; } Module B: module b; struct S {} void swap( inout S s1, inout S s2 ) { S t = s1; s1 = s2; s2 = t; } Main: import a; import b; template func( T ) { void func( inout T t1, inout T t2 ) { swap( t1, t2 ); } } void main() { int i = 1, j = 2; func( i, j ); } C:\code\d>dmd test a b a.d(3): function a.swap conflicts with b.swap at b.d(5) test.d(15): template instance test.func!(int) error instantiating While I haven't run up against this situation yet, I can see it being an obstacle for large projects. How would I resolve something like this? And I expect the overloading rules will be the same for template functions as they are for normal functions? Finally, is there a more D-oriented approach that might address this without language changes? I suppose one language change that might help would be to support the externally defined method syntax for primitives, so func could be rewritten as: template func( T ) { void func( inout T t1, inout T t2 ) { t1.swap( t2 ); } } and the specialized swap for struct S could be placed within its own scope. However, this still doesn't address all situations, and I'm not entirely certain that it wouldn't introduce new overloading problems where none existed before. SeanDoes this work? import a; import b; alias a.swap swap; alias b.swap swap; template func( T ) { void func( inout T t1, inout T t2 ) { swap( t1, t2 ); } } void main() { int i = 1, j = 2; func( i, j ); } -- Carlos Santander Bernal
Mar 09 2006
Carlos Santander wrote:Does this work? import a; import b; alias a.swap swap; alias b.swap swap;It does. alias is just too darn flexible for its own good :-) Sean
Mar 09 2006
Sean Kelly wrote:Carlos Santander wrote:By the way, the more complex C++ examples don't work in D anyway, because D is module-based. For example, it's more likely func would be defined in a third module like so: Module C: module c; import b; // let's say b is the default implementation template func( T ) { void func( inout T t1, inout T t2 ) { swap( t1, t2 ); } } Main: import a; import c; void main() { int i = 1, j = 2; func( s, t ); } However, this doesn't work in D even without overloading. So perhaps it's not much of an issue here anyway. SeanDoes this work? import a; import b; alias a.swap swap; alias b.swap swap;It does. alias is just too darn flexible for its own good :-)
Mar 09 2006
Sean Kelly escribió:By the way, the more complex C++ examples don't work in D anyway, because D is module-based. For example, it's more likely func would be defined in a third module like so: Module C: module c; import b; // let's say b is the default implementation template func( T ) { void func( inout T t1, inout T t2 ) { swap( t1, t2 ); } } Main: import a; import c; void main() { int i = 1, j = 2; func( s, t ); } However, this doesn't work in D even without overloading. So perhaps it's not much of an issue here anyway. SeanThis one, I don't understand. I don't have DMD to try the code, but I think the call sequence would be something like: main -> c.func -> b.swap The main module imports a, but it's never used. The only thing that I can think of is that you want c.func to use a.swap because it was imported in main, but it doesn't make much sense (for me, anyway) because c never knew about a. I don't know if C++ allows that, but I would say it's not a very good design. There should be another way. IMHO. -- Carlos Santander Bernal
Mar 09 2006
Carlos Santander wrote:Sean Kelly escribió:The above example doesn't really make sense in D but it's quite common in C++, whose current import mechanism is little more than a structured copy/paste during preprocessing (recall that module A defines the overload that Main wants to be used by module C). But this is semantically meaningless in D, which was my only point--that perhaps C++ overload resolution really isn't necessary in D after all.By the way, the more complex C++ examples don't work in D anyway, because D is module-based. For example, it's more likely func would be defined in a third module like so: Module C: module c; import b; // let's say b is the default implementation template func( T ) { void func( inout T t1, inout T t2 ) { swap( t1, t2 ); } } Main: import a; import c; void main() { int i = 1, j = 2; func( s, t ); } However, this doesn't work in D even without overloading. So perhaps it's not much of an issue here anyway. SeanThis one, I don't understand. I don't have DMD to try the code, but I think the call sequence would be something like: main -> c.func -> b.swap The main module imports a, but it's never used.The only thing that I can think of is that you want c.func to use a.swap because it was imported in main, but it doesn't make much sense (for me, anyway) because c never knew about a. I don't know if C++ allows that, but I would say it's not a very good design. There should be another way. IMHO.It's not a good design, but the C/C++ people are stuck with it :-) There's been a recent push to adopt a module import scheme for C++, but I think the huge mass of legacy code like the above will make the scheme very difficult to use. Sean
Mar 09 2006
"Sean Kelly" <sean f4.ca> wrote in message news:duqh7n$1l6c$1 digitaldaemon.com...There's been a recent push to adopt a module import scheme for C++, but I think the huge mass of legacy code like the above will make the scheme very difficult to use.I've seen that scheme. It strikes me as excessively complicated. Furthermore, there are no implementations of it, so nobody knows if it is implementable (see "export"), or if it is actually usable (see "namespace").
Mar 09 2006
In article <dur2g6$29us$1 digitaldaemon.com>, Walter Bright says..."Sean Kelly" <sean f4.ca> wrote in message news:duqh7n$1l6c$1 digitaldaemon.com...The proposal is actually pretty good about backward compatibility. The transition would be messy, yes, but transitions always are.There's been a recent push to adopt a module import scheme for C++, but I think the huge mass of legacy code like the above will make the scheme very difficult to use.I've seen that scheme. It strikes me as excessively complicated. Furthermore, there are no implementations of it, so nobody knows if it is implementable (see "export"), or if it is actually usable (see "namespace").In fairness, the proposal is by Daveed Vandevoorde of EDG, who AFAIK are the only people on the planet who *have* implemented "export". I don't think he's chucking this stuff out of the window of some ivory tower. My main concern about the proposal as it currently stands is that I can't see how build tools like "make" can possibly work with it. The model is no longer "compile a bunch of separate objects and throw them at the linker". To compile a source file you need to have compiled all source files it depends on, and all source files *they* depend on, and so on. I'm increasing worried that the whole module concept may not be workable in a language that doesn't allow references to undeclared symbols at compile time. cheers Mike
Mar 10 2006
Mike Capp wrote:My main concern about the proposal as it currently stands is that I can't see how build tools like "make" can possibly work with it. The model is no longer "compile a bunch of separate objects and throw them at the linker". To compile a source file you need to have compiled all source files it depends on, and all source files *they* depend on, and so on. I'm increasing worried that the whole module concept may not be workable in a language that doesn't allow references to undeclared symbols at compile time.This is my concern as well. Just how far will they truly be able to abstract themselves from the include model? However, I haven't yet read the proposal so perhaps I should do so before speculating further. Sean
Mar 10 2006
"Mike Capp" <mike.capp gmail.com> wrote in message news:durud0$109b$1 digitaldaemon.com...In article <dur2g6$29us$1 digitaldaemon.com>, Walter Bright says...I have the greatest admiration and respect for Daveed. But nobody is smart enough to reliably predict how useful such a complex addition will be in practice, or what all the corner cases will be, or how difficult it is to implement on any compiler other than his own, etc., all based on a paper proposal. Bottom line: the committee's track record on invention of complex new features is poor when they got standardized with no existing implementation experience (see namespace and export). The committee's record on standardizing functionality that *has* been extensively tried out is reasonably good (see STL).I've seen that scheme. It strikes me as excessively complicated. Furthermore, there are no implementations of it, so nobody knows if it is implementable (see "export"), or if it is actually usable (see "namespace").In fairness, the proposal is by Daveed Vandevoorde of EDG, who AFAIK are the only people on the planet who *have* implemented "export". I don't think he's chucking this stuff out of the window of some ivory tower.My main concern about the proposal as it currently stands is that I can't see how build tools like "make" can possibly work with it. The model is no longer "compile a bunch of separate objects and throw them at the linker". To compile a source file you need to have compiled all source files it depends on, and all source files *they* depend on, and so on. I'm increasing worried that the whole module concept may not be workable in a language that doesn't allow references to undeclared symbols at compile time.I.e., it's a design for something that is complicated and quite unique among programming languages. It will *fundamentally* change the way C++ programs are built, probably in unanticipated ways. There's no implementation experience to see how it will work in a real project. The risk is that if it gets standardized without being tried, and then people find out that it is either unworkable, unusable, or has little benefit in the real world, then it cannot be taken out again (like export).
Mar 10 2006
In article <dusk1e$24pt$1 digitaldaemon.com>, Walter Bright says... [various good points with with I fully agree]I.e., it's a design for something that is complicated and quite unique among programming languages. It will *fundamentally* change the way C++ programs are built, probably in unanticipated ways. There's no implementation experience to see how it will work in a real project.I think that statement is a little extreme. Daveed is arguably just trying to standardize and generalize the concept of precompiled headers, of which there's quite a lot of implementation experience. (Most of it bad, from where I've been standing.) Modules are a very different beast, granted, but not an entirely new one. cheers Mike
Mar 10 2006
Walter Bright wrote:"Mike Capp" <mike.capp gmail.com> wrote in message news:durud0$109b$1 digitaldaemon.com...Responding to both what Walter and Mike said: degree) do? To compile a .d source file, the compiler must parse all other .d files that the original one references (in the form of modules), so isn't it the same? What's the whole new different paradigm? -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#DIn article <dur2g6$29us$1 digitaldaemon.com>, Walter Bright says...I have the greatest admiration and respect for Daveed. But nobody is smart enough to reliably predict how useful such a complex addition will be in practice, or what all the corner cases will be, or how difficult it is to implement on any compiler other than his own, etc., all based on a paper proposal. Bottom line: the committee's track record on invention of complex new features is poor when they got standardized with no existing implementation experience (see namespace and export). The committee's record on standardizing functionality that *has* been extensively tried out is reasonably good (see STL).I've seen that scheme. It strikes me as excessively complicated. Furthermore, there are no implementations of it, so nobody knows if it is implementable (see "export"), or if it is actually usable (see "namespace").In fairness, the proposal is by Daveed Vandevoorde of EDG, who AFAIK are the only people on the planet who *have* implemented "export". I don't think he's chucking this stuff out of the window of some ivory tower.My main concern about the proposal as it currently stands is that I can't see how build tools like "make" can possibly work with it. The model is no longer "compile a bunch of separate objects and throw them at the linker". To compile a source file you need to have compiled all source files it depends on, and all source files *they* depend on, and so on. I'm increasing worried that the whole module concept may not be workable in a language that doesn't allow references to undeclared symbols at compile time.I.e., it's a design for something that is complicated and quite unique among programming languages. It will *fundamentally* change the way C++ programs are built, probably in unanticipated ways. There's no implementation experience to see how it will work in a real project. The risk is that if it gets standardized without being tried, and then people find out that it is either unworkable, unusable, or has little benefit in the real world, then it cannot be taken out again (like export).
Mar 11 2006
D doesn't allow overloading of ordinary functions with template functions. C++ does, but I don't see a compelling reason for it, and it is the source of some complicated corner cases.
Mar 09 2006
Walter Bright wrote:D doesn't allow overloading of ordinary functions with template functions. C++ does, but I don't see a compelling reason for it, and it is the source of some complicated corner cases.Yup, and I agree that this is a good thing. My example used normal functions because template overloading is incomplete. Sean
Mar 09 2006
"Sean Kelly" <sean f4.ca> wrote in message news:dur5fn$2g79$1 digitaldaemon.com...My example used normal functions because template overloading is incomplete.That'll get fixed.
Mar 09 2006
"Sean Kelly" <sean f4.ca> wrote in message news:duq8gt$1cav$1 digitaldaemon.com...By the way, the more complex C++ examples don't work in D anyway, because D is module-based. For example, it's more likely func would be defined in a third module like so: Module C: module c; import b; // let's say b is the default implementation template func( T ) { void func( inout T t1, inout T t2 ) { swap( t1, t2 ); } } Main: import a; import c; void main() { int i = 1, j = 2; func( s, t ); } However, this doesn't work in D even without overloading.I doubt that this would work in any programming language ;-)
Mar 10 2006
On Thu, 09 Mar 2006 13:36:25 -0800, Sean Kelly wrote:Carlos Santander wrote:However, this is exactly one of the reasons that alias was invented by Walter - to explicitly disambiguate identifiers that reside in different modules. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 10/03/2006 9:20:29 AMDoes this work? import a; import b; alias a.swap swap; alias b.swap swap;It does. alias is just too darn flexible for its own good :-)
Mar 09 2006
"Sean Kelly" <sean f4.ca> wrote in message news:duq74p$1aoo$1 digitaldaemon.com...Carlos Santander wrote:Actually, alias was specifically designed so that will work. It enables one to select which groups of functions one wants to overload with, rather than doing C++ style ADL.Does this work? import a; import b; alias a.swap swap; alias b.swap swap;It does. alias is just too darn flexible for its own good :-)
Mar 09 2006