12

I have used pragmas in Pharo Smalltalk and have an idea about how they work and have seen examples for what they are used in Pharo.

My questions are:

  • what are pragmas conceptually,
  • to what construct do they compare in other languages,
  • when should i introduce a pragma?

I already found an interesting article about their history: The history of VW Pragmas.

4

1 回答 1

17

You must think of it as Annotations attached to a CompiledMethod, or if you want as additionnal properties.

Then, thanks to reflection, some tools can walk other compiled methods, collect those with certain annotations (properties) and apply some special handling, like constructing a menu, a list of preferences, or other UI, invoking every class methods marked as #initializer, or some mechanism could be walking the stack back until a method is marked as an #exceptionHandler ...

There are many possibilities, up to you to invent your own meta-property...

EDIT

For the second point, I don't know, it must be a language that can enumerate the methods, and can attach properties to them.

The third point is also hard to answer. In practice, I would say you would use some already existing annotations, but very rarely create a new one, unless you're trying to create a new framework for exception handling, or a new framework for GUI (you want to register some known events or some handlers...). The main usage I would see is for extending, composing an application with unrelated parts, like a main menu. It seems like a relatively un-intrusive way to introduce DECLARATIVE hooks - compared to the very intrusive way to override a well known method TheWorld>>mainMenu. It's also a bit lighter than registering/un-registering IMPERATIVELY via traditional message send at class initialization/unoading. On the other hand, the magic is a bit more hidden.

于 2013-11-13T17:43:19.303 回答