[[Property:title|What makes a Certified Attachment Pattern]] [[Property:weight|8]] [[Property:uuid|1a20197d-5a88-59c3-9a04-512399125661]] {{underconstruction}} ==A little background on CAPs== Certified Attachment Patterns (CAPs) were described in the section on [[Void-safety: Background, definition, and tools#Certified attachment patterns (CAPs)|void-safety tools]]. To review, a CAP is a code pattern for a certain expression, say exp of a detachable type that ensures that exp will never have a void run-time value within the covered scope. A simple example is the familiar test for void reference: if l_x /= Void then l_x.do_something end We know that after the explicit check to make sure l_x is not Void, that the feature application l_x.do_something is void-safe. Of course, you should remember from previous discussions that l_x must be a local variable, a formal argument, or a [[Void-safety: Background, definition, and tools#Stable attributes|stable attribute]]. When void-safety was first envisioned for Eiffel, it was intended that individual CAPs would be proven or certified and documented. This would produce a "catalog" of CAPs. What happened instead is that the members of the Eiffel standards committee have been able to produce and publish as part of the [http://www.ecma-international.org/publications/standards/Ecma-367.htm standard] a definition of the nature of a CAP from which a determination can be made as to whether a particular code pattern is or is not a CAP. The definition in the standard document is not easily readable by most developers. So, in this documentation, you will see various examples of CAPs and the rationale behind them. ==The standard CAP definition== The Eiffel standard (2nd edition, June 2006) defines a CAP as follows: ---- '''''A Certified Attachment Pattern (or CAP) for an expression exp whose type is detachable is an occurrence of exp in one of the following contexts: ''''' '''''1. exp is an Object-Test Local and the occurrence is in its scope. ''''' '''''2. exp is a read-only entity and the occurrence is in the scope of a void test involving exp.''''' ---- The terminology used in the definition is precise. For example, terms like "read-only entity" and "scope of a void test" have specific meanings that are supported by their own definitions in the standard. Still, the standard does contain informative text that gives us a guideline that a CAP is a scheme to ensure that a particular expression of a detachable type will never have void run-time value in the scope covered by the CAP. The discussion here will follow that guideline, and, as such, will be less formal (and consequently less precise) than that in the standard, and is intended to be a practical guide. Of course, the [http://www.ecma-international.org/publications/standards/Ecma-367.htm standard document] is available for download if you wish to investigate the specifics. ==CAP-able expressions== In the first context in the definition above, the expression exp can be an '''Object-Test Local'''. An Object-Test Local is the identifier specified for a fresh local entity in an '''object test'''. Remember, object tests are coded using the [[Void-safety: Background, definition, and tools#The attached syntax (object test)|attached syntax]]. attached x as l_x In the object test expression above, the identifier '''l_x''' is an Object-Test Local. In the second context, the expression can be a '''read-only entity'''. Read-only entities are: # Constant attributes # Formal arguments # Object-Test Locals # Current Additionally, the Eiffel Software compiler allows for [[Void-safety: Background, definition, and tools#Stable attributes|stable attributes]] and local variables to be protected by a CAP. Stable attributes are the only class attributes which are CAP-able. This is because stable attributes, once attached at run-time, can never have a void value again. Local variables can be used in a CAP as long as they are not the target of an assignment whose source is Void or some expression which could possibly be void. So, for a local variable l_string, the following is valid: local l_string: detachable STRING do if l_string /= Void then l_string.append ("xyz") -- Valid ... But, if l_string had been a target of an assignment in which the source could possibly have been void, then it could no longer be guaranteed that l_string is non-void. So, assuming that my_detachable_string is an attribute declared as type detachable STRING, the code in this example would be invalid: local l_string: detachable STRING do if l_string /= Void then l_string := my_detachable_string l_string.append ("xyz") -- Invalid: my_detachable_string could be void ...