Function prototype + definition in the same file

Steven Schveighoffer schveiguy at yahoo.com
Tue Sep 25 13:43:24 PDT 2012


On Tue, 25 Sep 2012 16:07:51 -0400, Jonathan M Davis <jmdavisProg at gmx.com>  
wrote:

> 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.

I misunderstood, I was thinking in terms of generated code.

But in any case, it's still not duplicated code.  What you are doing is  
defining an interface, then attaching the implementation of the interface  
to the declaration by repeating its name.

Would you say repeating the function signature of an interface in an  
implementing class is code duplication?  Would you say that typing the  
name of a variable that you already declared so you could access it is  
code duplication?

Duplicated or not, it's still valid under the spec as far as I can tell.

> 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++.

The main reason for having prototypes in C/C++ was so you could declare a  
symbol that you weren't defining yet.

In some cases, this was necessary, because C/C++ does not have forward  
references.  So there is indeed very little reason in D to declare a  
function, then implement it in the same file.

But I think what Manu is trying to do is not exactly just "repeat the  
signature", he is using the signature of the original to generate the  
signature of the auto-generated function.  Essentially, it's not  
duplicated code on either the source or the compiled level, so disabling  
the ability to declare a function and then implement it later would  
prevent this possibility.  I don't think it makes sense to do that.

-Steve


More information about the Digitalmars-d mailing list