Interfaces are reference counted types. That means that if the reference count is zero, the compiler will clean up the underlying class.
You have to be a clean and careful programmer to be consistent: ALWAYS instantiate to an interface and NEVER to a class.
People often make that mistake, use the interface (works) but if it is a class instance you need to free it.
When you consistently use interfaces, everything is auto-free'd
BTW:
invariant return types are to some extend supported in multiple ways:
- Methods do not change their signature when part of an interface
- Methods can be overridden with the same signature and not call inherited
- Use TClass instead of TObject as return value: callee can reconstruct.
- Use RTTI
- Typesafe softcasts (is to test, as to use)
My example is an example of an invariant return type....
Usually fully compiled languages can not achieve the same flexibility as Java or C# which are half way scripting languages.
But if you stick to interfaces you can achieve a lot.