www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Feature request: function prototypes

reply Robert Fraser <fraserofthenight gmail.com> writes:
This may seem like a pretty stupid request, but since I use a lot of 
version() statements in my code; it'd be really cool to have something 
like function signatures at the top of my file. When I declare my 
versioned functions, I'd love to be able to have them checked against 
prototypes declared above their use, as a first line of defense (so I 
don't need to r-test it on different OSes and libraries if I make an 
obvious change to one of the signatures or something).
Feb 08 2008
next sibling parent reply Christopher Wright <dhasenan gmail.com> writes:
Robert Fraser wrote:
 This may seem like a pretty stupid request, but since I use a lot of 
 version() statements in my code; it'd be really cool to have something 
 like function signatures at the top of my file. When I declare my 
 versioned functions, I'd love to be able to have them checked against 
 prototypes declared above their use, as a first line of defense (so I 
 don't need to r-test it on different OSes and libraries if I make an 
 obvious change to one of the signatures or something).

Have a unittest? To satisfy your purposes, it just needs to call each function, even with invalid input. Then if everything compiles with -unittest, you're golden, even if that crashes. Satisfies your requirements, anyway, and in any situation, you'd be duplicating the signature three times.
Feb 08 2008
next sibling parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
Christopher Wright wrote:
 Robert Fraser wrote:
 This may seem like a pretty stupid request, but since I use a lot of 
 version() statements in my code; it'd be really cool to have something 
 like function signatures at the top of my file. When I declare my 
 versioned functions, I'd love to be able to have them checked against 
 prototypes declared above their use, as a first line of defense (so I 
 don't need to r-test it on different OSes and libraries if I make an 
 obvious change to one of the signatures or something).

Have a unittest? To satisfy your purposes, it just needs to call each function, even with invalid input. Then if everything compiles with -unittest, you're golden, even if that crashes. Satisfies your requirements, anyway, and in any situation, you'd be duplicating the signature three times.

But unittests are run with specific version flags. I think he means something like this: --- prototype { int someFunc(float, bool); } version(one) { int someFunc(float f, bool b) { // ... } } else { int someFunc(float f, bool b) { // ... } } --- Now, if you change the signature of someFunc inside of version(one) you'll get a compiler error, because the prototype "int someFunc(float, bool)" would not be found if version is "one". Alternatively, if you change the prototype inside the "prototype { }", you'll get two compiler errors: in "one" the prototype doesn't exist, neither in the "else". That way you can maintain synchronized the two version blocks. The problem is, how to implement this? The compiler would need to check all possible combinations of versions, debugs, static ifs, etc. By the way, this will also allow tools to provide good refactoring support.
Feb 08 2008
parent BCS <BCS pathlink.com> writes:
Ary Borenszweig wrote:

 
 But unittests are run with specific version flags.
 
 I think he means something like this:
 
 ---
 prototype {
   int someFunc(float, bool);
 }
 
 version(one) {
   int someFunc(float f, bool b) {
     // ...
   }
 } else {
   int someFunc(float f, bool b) {
     // ...
   }
 }
 ---

To get the full advantage of this you would need to be able to run the compiler with "wrong" versions. e.g. on windows, run the compile up to the code generation phase with the Linux versions set. This in and of it's self might be handy.
Feb 08 2008
prev sibling parent Jesse Phillips <jessekphillips gmail.com> writes:
On Fri, 08 Feb 2008 12:44:04 -0200, Ary Borenszweig wrote:

 Christopher Wright wrote:
 Robert Fraser wrote:
 This may seem like a pretty stupid request, but since I use a lot of
 version() statements in my code; it'd be really cool to have something
 like function signatures at the top of my file. When I declare my
 versioned functions, I'd love to be able to have them checked against
 prototypes declared above their use, as a first line of defense (so I
 don't need to r-test it on different OSes and libraries if I make an
 obvious change to one of the signatures or something).

Have a unittest? To satisfy your purposes, it just needs to call each function, even with invalid input. Then if everything compiles with -unittest, you're golden, even if that crashes. Satisfies your requirements, anyway, and in any situation, you'd be duplicating the signature three times.

But unittests are run with specific version flags. I think he means something like this: --- prototype { int someFunc(float, bool); } version(one) { int someFunc(float f, bool b) { // ... } } else { int someFunc(float f, bool b) { // ... } } --- Now, if you change the signature of someFunc inside of version(one) you'll get a compiler error, because the prototype "int someFunc(float, bool)" would not be found if version is "one". Alternatively, if you change the prototype inside the "prototype { }", you'll get two compiler errors: in "one" the prototype doesn't exist, neither in the "else". That way you can maintain synchronized the two version blocks. The problem is, how to implement this? The compiler would need to check all possible combinations of versions, debugs, static ifs, etc. By the way, this will also allow tools to provide good refactoring support.

If that is the case I would much rather have the prototype leave off the parameter-list and only require all declarations to be the same.
Feb 08 2008
prev sibling next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Robert Fraser" <fraserofthenight gmail.com> wrote in message 
news:fohlg3$acr$1 digitalmars.com...
 This may seem like a pretty stupid request, but since I use a lot of 
 version() statements in my code; it'd be really cool to have something 
 like function signatures at the top of my file. When I declare my 
 versioned functions, I'd love to be able to have them checked against 
 prototypes declared above their use, as a first line of defense (so I 
 don't need to r-test it on different OSes and libraries if I make an 
 obvious change to one of the signatures or something).

Would it be possible to put the version statement inside the function bodies? Yes, it's somewhat more typing, but it means you only have to actually declare each function once (and write ddoc for them once, as a bonus).
Feb 08 2008
parent reply Robert Fraser <fraserofthenight gmail.com> writes:
Jarrett Billingsley wrote:
 "Robert Fraser" <fraserofthenight gmail.com> wrote in message 
 news:fohlg3$acr$1 digitalmars.com...
 This may seem like a pretty stupid request, but since I use a lot of 
 version() statements in my code; it'd be really cool to have something 
 like function signatures at the top of my file. When I declare my 
 versioned functions, I'd love to be able to have them checked against 
 prototypes declared above their use, as a first line of defense (so I 
 don't need to r-test it on different OSes and libraries if I make an 
 obvious change to one of the signatures or something).

Would it be possible to put the version statement inside the function bodies? Yes, it's somewhat more typing, but it means you only have to actually declare each function once (and write ddoc for them once, as a bonus).

That's what I'm mostly doing now, but I'd rather be able to group stuff together (i.e. "here's the Tango functions, here's the Phobos ones), and Ddoc the stuff up with the prototypes at the top. But maybe it's not a big enough deal to justify a language change.
Feb 08 2008
parent "Bruce Adams" <tortoise_74 yeah.who.co.uk> writes:
On Fri, 08 Feb 2008 18:58:14 -0000, Robert Fraser  
<fraserofthenight gmail.com> wrote:

 Jarrett Billingsley wrote:
 "Robert Fraser" <fraserofthenight gmail.com> wrote in message  
 news:fohlg3$acr$1 digitalmars.com...
 This may seem like a pretty stupid request, but since I use a lot of  
 version() statements in my code; it'd be really cool to have something  
 like function signatures at the top of my file. When I declare my  
 versioned functions, I'd love to be able to have them checked against  
 prototypes declared above their use, as a first line of defense (so I  
 don't need to r-test it on different OSes and libraries if I make an  
 obvious change to one of the signatures or something).

bodies? Yes, it's somewhat more typing, but it means you only have to actually declare each function once (and write ddoc for them once, as a bonus).

That's what I'm mostly doing now, but I'd rather be able to group stuff together (i.e. "here's the Tango functions, here's the Phobos ones), and Ddoc the stuff up with the prototypes at the top. But maybe it's not a big enough deal to justify a language change.

Would it be overkill to use interfaces and several separate implementation classes instead? That is what polymorphism is about after all. Also you would be able to refactor any common code which is hard to do with version statements all over the place. Bruce.
Feb 09 2008
prev sibling parent Russell Lewis <webmaster villagersonline.com> writes:
Robert Fraser wrote:
 This may seem like a pretty stupid request, but since I use a lot of 
 version() statements in my code; it'd be really cool to have something 
 like function signatures at the top of my file. When I declare my 
 versioned functions, I'd love to be able to have them checked against 
 prototypes declared above their use, as a first line of defense (so I 
 don't need to r-test it on different OSes and libraries if I make an 
 obvious change to one of the signatures or something).

This sounds to me like a pretty compelling argument for function prototypes. An ugly but workable solution to this would be static asserts which verified the type of each function.
Feb 08 2008