Function prototype + definition in the same file

Jonathan M Davis jmdavisProg at gmx.com
Tue Sep 25 13:07:51 PDT 2012


On Tuesday, September 25, 2012 15:48:11 Steven Schveighoffer wrote:
> On Tue, 25 Sep 2012 15:23:50 -0400, deadalnix <deadalnix at gmail.com> wrote:
> > Le 25/09/2012 20:57, Maxim Fomin a écrit :
> >> On Tuesday, 25 September 2012 at 17:48:46 UTC, deadalnix wrote:
> >>> This is code duplication and is generally considered as a bad
> >>> practice. I'm not convinced that the language should be modified to
> >>> allow something that is known as bad practice.
> >>> 
> >>> What you need here is a more robust di generator IMO.
> >> 
> >> This is neither a code duplication (there is certain distinction between
> >> prototype and body) nor a bad practice - this is a common pattern in C
> >> and C++. It is surprising that D cannot do such a simple thing which in
> >> addition is not harmless, so there is no sense in depreciating it.
> > 
> > This is code duplication. And this has been considered a problem in
> > C/C++ for a while now.
> 
> No, it's a prototype.
> 
> Note that it's *required* if you want to hide implementation using .di
> files.
> 
> There is no code involved, it's just generating a symbol reference in the
> object file.

It's still code duplication, because you've now listed the function signature 
twice. Yes, it's minimal code duplication, but it's still duplicating code. 
It's necessary for stuff like .di files (which are code duplication by 
definition, because they're duplicating a module - or at least part of it), but 
most everyone I know has thought (where it's come anyway) that prototypes were 
a _bad_ thing about C/C++. So, eliminating them as much as possible would 
generally be good, and even when it comes to .di files, given their limitations 
(e.g. disabling inlining and CTFE), we arguably should have a better solution 
for interface files anyway (e.g. having a binary file with the partially 
compiled code and generated documentation alongside it giving the API), in 
which case we wouldn't even need prototypes for interface files anymore. If
anything, I'd argue that the fact that prototypes are part of the language is
a sign of a deficiency in the language (especially if they're necessary).

- Jonathan M Davis


More information about the Digitalmars-d mailing list