tags:

views:

167

answers:

3

Almost all of the Delphi code I have read has all the class type definitions in the units interface section, but I have seen occasional use of type definitions within the implementation section.

What exactly is the difference between these, and why would I use this?

+10  A: 

It's pretty simple: types defined in implementation only are only visible within the implementation, so they cannot be used as types of arguments or return values in the interface. So, position your type definitions (like anything else;-) based on whether those types are only an implementation detail, or something you want to make visible externally, i.e., through the interface!

Alex Martelli
Ok, so is there any overhead or saving in this, or is it purely a matter of information hiding? I have, as mentioned, not actually seen it used very often, which seems strange if it is 'better practice'.
HMcG
@HMcG, no intrinsic overhead either way -- I don't know why you've seen it used rarely, maybe people don't think about it (or rarely need extra types purely for implementation purposes).
Alex Martelli
It's purely a scope issue. Expose only what needs to be exposed. What's yours is yours. What's mine is mine. Unless we decide to share.
Cape Cod Gunny
Thanks both, that clarifies things. I have been in the practice of putting all class type defs into the interface section as that is how most examples show things. A classic case of cargo cult programming - I just had not really thought about it. Cheers.
HMcG
@HMcG, the important thing is that now you HAVE thought about it critically, asked just the right question, and learned thereby -- upwards and onwards!-)
Alex Martelli
There actually is an overhead involved: If you change the interface section of a unit, the compiler will recompile all units that use this unit which in turn will force it to recompile all units that use these recompiled units. A change in the implementation section does not trigger this recompile. But that's only a compile time issue, there is no runtime overhead involved and even the compile time does not matter that much given the speed of the Delphi compiler.
dummzeuch
Well, if the unit is used in a runtime package, then making changes to a class that is defined in the interface section will mean that all packages, DLLs and applications that use that runtime package will also have to be recompiled. You can make changes to classes in the implementation section without compromising package compatibility.
Oliver Giesen
+4  A: 

Scope. Interface declarations are public and availabe to other units when that unit is include in the Uses clause. Implementation declarations are private and only available within that specific Unit.

Cape Cod Gunny
+2  A: 

There is a general difference between code changes in interface and the code changes implementation during compilation. If you add a class to or change an existing class in the interface section then every unit that references the changed unit will need to be recompiled. However a change in the implementation section (a new subclass or code changes) will only required the recompilation of that unit and the IDE will link the previously compiled DCU plus the new one together to create the EXE file.

Overall the major benefit, is that it allows you to design you code to hide implementation details - define the parent class in the interface and any subclasses in the implementation. Or define classes in the implementation if they are solely needed to implement the behavious of a class/method available in the interface section.

David Gray