9

Is there any compiler options that let the compiler give me an error instead of a warning when i instantiate an abstract class?

Foo = class
    procedure Bar; virtual; abstract;
end;

var
    f : Foo;
begin
    f := Foo.Create;  // <-- should give me a compile time error
end;
hansmaad
  • 18,417
  • 9
  • 53
  • 94
  • +1 I would appreciate that as well! – jpfollenius Jan 07 '11 at 10:03
  • 1
    And I wouldn't... :) So be glad that we can customize this behaviour. And @Deltics' answer shows us how. – Marjan Venema Jan 07 '11 at 10:15
  • I fail to get excited by this. If you need to detect this at compile time then your bigger problem is that your test code is incomplete. That's actually what you need to be worrying about! – David Heffernan Jan 07 '11 at 10:26
  • Warning level is OK. Good to have it customizable, however such liberties are creating schisms and heresies. – Free Consulting Jan 07 '11 at 10:27
  • 2
    @David: IMHO it is always better to find problems at compile time if it's possible so this is a legitimate question. – jpfollenius Jan 07 '11 at 10:27
  • @David: I'm working on a big legacy project which gives me hundreds of warnings after porting to Delphi2010. The project does not have any tests. I think such an instantiation is an error and it could be detected at compile time. That's what I'm used to get and i don't know why i should want to run such code. – hansmaad Jan 07 '11 at 10:58
  • @hansmaad It's only a problem if you call Bar on class Foo, or a subclass that doesn't override Bar, but you already know that. Since you have a long-existing legacy project, my guess is that the code doesn't call Bar on the Foo instances. If you turn this into a warning and force yourself to make significant changes, you may introduce bugs! Since you are clearly developing the code further, you need to tackle the underlying problem – the lack of tests. – David Heffernan Jan 07 '11 at 11:25
  • @Smasher I agree with that point, I agree that it's a good question. All I'm trying to say, is that forcing a compile time error here, in itself, is not a panacea. – David Heffernan Jan 07 '11 at 11:33
  • Similar question: http://stackoverflow.com/questions/3120220/delphi-6-force-compiler-error-on-missing-abstract-class-methods – Cosmin Prund Jan 07 '11 at 12:22
  • @Cosmin but it's worth also pointing out that the accepted answer in the question to which you linked is utterly bogus – David Heffernan Jan 07 '11 at 12:37
  • It is good it being an option, because when you're designing and implementing a library you would like to be able to compile it even if all the abstract methods are not implemented yet. Being forced to write a lot of empty methods is worse than having a run-time exception, which at least tell you something went wrong and doesn't hide the issue. Of course being able to get an error when the library is finished before deploying is welcome. –  Jan 07 '11 at 14:19
  • @David - What is bogus about my answer on the linked question? It is a technique foe use in circumstances where Delphi cannot detect that there will be an abstract error (mainly using "class of TMyObject" coding, such as the (T)ItemClass in TCollection (ok, there are no abstract methods in TCollectionItem, but I sometimes use similar techniques with partially abstract classes. – Gerry Coll Jan 07 '11 at 21:59
  • @Gerry In your example, who says that you are actually creating instances of those classes elsewhere? Why do you need to have the CheckAbstracts conditional code? You just check for warnings with your normal code. It's utterly pointless. – David Heffernan Jan 07 '11 at 22:52
  • @David - not if it is unclear what classes will be instantiated, it normally if I use code like `NewItem := GetClassType.Create`, rather than `NewItem := TItemClass.Create`. If a new abstract method is added to the base class, there will be no compiler warnings issued as it doesn't know which subclass you are using. Of course in D2009+ it may be better to use Generics for this type of code. – Gerry Coll Jan 08 '11 at 01:47
  • For command-line compilation see http://stackoverflow.com/questions/15383471/is-there-a-dcc32-option-to-treat-a-specific-compiler-warning-as-an-error – mjn Mar 13 '13 at 11:55

1 Answers1

18

In Delphi 2010 (at least, possibly earlier versions but cannot test/verify):

Project -> Options

  + Delphi Compiler

     > Hints and Warnings

         + Output Warnings:

              Constructing instance containing abstract method:  ___________

Change this setting from "True" to "Error"

Deltics
  • 22,162
  • 2
  • 42
  • 70
  • 1
    Yep - I hated having to direct someone to use the Project Options dialog... it is an abomination since they "improved it". – Deltics Jan 07 '11 at 10:44
  • True, "improved" one is effectively harder to use than "old" one, with checkboxes and radiogroups (granted i can not imagine out of my head how to accomodate build configuration defaults into old-school dialog, but still, finding an option to toggle takes more time now) – Free Consulting Jan 07 '11 at 13:27
  • 1
    @Worm on the other hand you usually do this once and then forget all about it; there are other more important areas to work on; just be thankful that you don't have to use the MSVC configuration dialog! – David Heffernan Jan 07 '11 at 14:01
  • @David - which is just as good an argument for them having left it alone... rather than re-engineer the whole thing to make option sets workable, at the expense of making it confusing and difficult to use generally, why not put up with confusing and difficult to use *option set* functionality ? But that's a discussion for else-when. :) – Deltics Jan 07 '11 at 19:18
  • @Deltics Option sets and msbuild are a huge improvement on the old D7 days where automating builds was very unsatisfactory – David Heffernan Jan 07 '11 at 19:21
  • FinalBuilder knocks MS Build into a cocked hat, and option sets could be had using plug-ins. Adding features and facilities is one thing (and no bad thing) but a Good Idea can be severely let down by poor execution, as in this case. Sadly. This is the 2nd question on SO in recent weeks asking how to do something that is actually dead easy, only made difficult and unintuitive by the poor implementation of this specific dialog. – Deltics Jan 07 '11 at 19:38
  • 2
    @David Heffernan hows "once"? it is per project compile-time configuration. Truly yours, Captain Obvious. – Free Consulting Jan 08 '11 at 09:48
  • @worm it's a text file, do it once, capture the settings and script the rest – David Heffernan Jan 08 '11 at 09:58
  • See also: [Is there a DCC32 option to treat a specific compiler warning as an error?](http://stackoverflow.com/questions/15383471/is-there-a-dcc32-option-to-treat-a-specific-compiler-warning-as-an-error) – mjn Mar 13 '13 at 11:54