0

What happens to a procedure when it is declared with the keyword dynamic?

And what is the effect of declaring it with the keyword static?

Michael Vincent
  • 1,620
  • 1
  • 20
  • 46
  • Not only are you asking two different questions, but both of them are in the docs. Your rep hints that you should know this by now. – Jerry Dodge Jan 09 '16 at 15:52
  • 4
    Typically mean and unhelpful developers surface whenever a Delphi question is asked. I can see why Embarcadero products are the way it is. Just a reflection... languages with a positive community flourish – reckface Jan 09 '16 at 15:56
  • I was thrown by the use of "procedure" in the question. – Michael Vincent Jan 09 '16 at 15:59
  • @reckface: On the other hand, maybe Delphi developers care about doing things the right way? In addition, reading the docs is a very good idea, although I agree you can inform the OP about this using a different tone. (Also, really, Jerry's message wasn't mean, I think.) – Andreas Rejbrand Jan 09 '16 at 16:01
  • 3
    @AndreasRejbrand I write in multiple languages and ask questions here, I just don't see the level of animosity toward learners in many other language tags. – reckface Jan 09 '16 at 16:04
  • 3
    It's really about how Stack Overflow works. Just pointing out two reasons why these types of questions should not be asked here. – Jerry Dodge Jan 09 '16 at 16:04
  • 1
    This is perfect example of bad question. Not because it asks something simple you can read in documentation - sometimes good answer on SO gives you way more than documentation can - but because you never bothered to actually try applying dynamic or static to regular procedures. That code would not even compile. – Dalija Prasnikar Jan 09 '16 at 17:18
  • I suggest you read [Stack Overflow question checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) – Dalija Prasnikar Jan 09 '16 at 17:36
  • 3
    @reckface : Agreed - There are people here with a vast amount of knowledge that do enormous damage to the Delphi community with their attitude. Sadly, instead of following the "be nice" rule http://stackoverflow.com/help/be-nice they downvote and criticise contributions from outside of their clique. They appear to have (and use) comment-deletion privilege or can always claim that criticism of their attitude is "off topic" or "violates the rules" and bother the moderators to stop the "bullying." Unfortunately, it's a waste of time trying to get them to listen. – Magoo Jan 09 '16 at 18:48
  • @Magoo Only diamond mods can delete comments. Ordinary users can flag abusive comments for deletion. Typically the comments that get deleted are grossly offensive. If you think that is bullying then you are out on a limb. – David Heffernan Jan 09 '16 at 19:08
  • @reckface, I'm not sure if you can say what *fungus is flourishing* in perfectly good English. Obviosly, **this question does not show any research effort**. I suggest everyone who advocates here to invest 10% of their efforts into improving this two-sentence question. – Free Consulting Jan 09 '16 at 19:25
  • Hover the mouse over the downvote arrow. The tooltip box that pops up says "This question does not show any research effort..." which is exactly why it's received 7 downvotes. – Jerry Dodge Jan 09 '16 at 19:32
  • @DavidHeffernan : Hmm - touched a nerve, have I? You may note that *bullying* is in quotes. Commentary on the attitude of some contributors - especially in the Delphi area - is described as "bullying" **by the bullies** in their complaints to the moderators so that the delicate little flowers can get remarks they do not appreciate deleted. In other areas, we don't see the Meta-style downvoting and negative **unhelpful** comments by the same old group - excused on the grounds that the question violates the rules while ignoring the "be nice" rule. – Magoo Jan 09 '16 at 19:51
  • 2
    @Magoo No nerves touched. I don't agree with that at all. If you have a problem take it up at meta. The Delphi tag is one of the lighter areas. Try hanging out in C and C++. Lots of straight talking there. – David Heffernan Jan 09 '16 at 20:28
  • @DavidHeffernan : Like others, I avoid the Delphi area here because of the hostility. The fact that the areas dedicated to wannabe languages are more hostile is irrelevant. You catch more flies with honey than with vinegar. What people do on SO is reflected far and wide. For instance, recent comments on ADUG : `I doubt that you're out of step with "the community" Roger, that's ????. 'Nuff said.` and `For once an acid comment from ???? was appropriate!` Being helpful and suppressing the urge to disparage others may add to notoriety, but doesn't further the cause. – Magoo Jan 09 '16 at 21:20
  • @Magoo Why not come over here and help? – David Heffernan Jan 09 '16 at 21:35
  • @DavidHeffernan : Like others, I avoid the Delphi area here because of the hostility. – Magoo Jan 09 '16 at 21:44
  • @Magoo I'm really not sure what you are getting at. Hostility is a strong word. Bullying is a strong accusation. Where is there bullying? – David Heffernan Jan 09 '16 at 22:11
  • 2
    @reckface: I watch the Delphi qs on SO daily and it's easy to spot most which are going to get down-voted - usually it's either because if no sign of research effort as JerryD mentioned, or (actually more frequently, it seems to me) lack of any effort to ask a clear question including the relevant details and/or lack of any debugging effort. – MartynA Jan 10 '16 at 09:22
  • 4
    @Magoo Try asking question like that (or any bad question) in Android or Java tag. You will get down voted into oblivion in matter of seconds. Delphi tag is pretty good. People here will offer suggestions to improve question and/or ask additional information before they down vote or even close vote. Unless question is unsalvageable, of course. – Dalija Prasnikar Jan 10 '16 at 09:27

1 Answers1

10

This question can be answered by reading the documentation.

The dynamic keyword introduces a method that can be overridden polymorphically. Semantically it is interchangeable with virtual, but the is implemented in a different manner. Read about it here: http://docwiki.embarcadero.com/RADStudio/en/Methods#Virtual_and_Dynamic_Methods

To make a method virtual or dynamic, include the virtual or dynamic directive in its declaration. Virtual and dynamic methods, unlike static methods, can be overridden in descendent classes. When an overridden method is called, the actual (run-time) type of the class or object used in the method call--not the declared type of the variable--determines which implementation to activate.

To override a method, redeclare it with the override directive. An override declaration must match the ancestor declaration in the order and type of its parameters and in its result type (if any).

...

In Delphi for Win32, virtual and dynamic methods are semantically equivalent. However, they differ in the implementation of method-call dispatching at run time: virtual methods optimize for speed, while dynamic methods optimize for code size.

In general, virtual methods are the most efficient way to implement polymorphic behavior. Dynamic methods are useful when a base class declares many overridable methods that are inherited by many descendent classes in an application, but only occasionally overridden.

Class static methods are like class methods in that they are invoked on the class rather than an instance. The difference between class static and class methods is that class methods are passed a Self pointer that contains the class, and class static methods are not. This means that class methods can be polymorphic and class static methods cannot. Read about it here: http://docwiki.embarcadero.com/RADStudio/en/Methods#Class_Static_Methods

Like class methods, class static methods can be accessed without an object reference. Unlike ordinary class methods, class static methods have no Self parameter at all. They also cannot access any instance members. (They still have access to class fields, class properties, and class methods.) Also unlike class methods, class static methods cannot be declared virtual.


With all due respect, I refer you to this question: How can I search for Delphi documentation?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thank you, David. I had read that. My question was related to procedures, not methods. I am wondering if there is a difference when applying the keyword to a procedure outside of a class. – Michael Vincent Jan 09 '16 at 15:54
  • 2
    The question does not state that. Furthermore, `dynamic` and `static` can only be applied to methods. – David Heffernan Jan 09 '16 at 15:55
  • 1
    @MichaelVincent, come on, you are asking about [syntax error](//ideone.com/BKJA7x) – Free Consulting Jan 09 '16 at 19:35
  • "wondering if there is a difference" - I think your misunderstanding may be arising from your assumption that static/dynamic can be applied to stand-alone procedures (and functions). Why should they be needed? They indicate to the compiler how to deal with dispatching a call to a method, but with stand-alone procedures in Delphi, that issue does not arise. – MartynA Jan 10 '16 at 09:18