J3/04-144 Date: 26 Dec. 2003 To: J3 From: Dan Nagle Subject: Proposed ASSERT in Type Definitions Placing assertions into programs serves at least three purposes: First, they provide a simple means of debugging; Second, they document the programmer's intentions; Third, they provide a means to allow the compiler gain information which might require entire application analysis, if it could be gleened at all. If assertions are accepted, they should be allowed to appear in derived type definitions, where they may specify a property assumed to be held for objects of that type. Number: Title: ASSERT in Type Definitions Submitted By: J3 Status: For Consideration References: Basic Functionality: Make an extension of the executable ASSERT statement to derived type definitions, where they can detect inappropriate values. Rationale: Extending assertions to derived type definitions can be used to monitor values assigned to variables. For example, a rank-2 two by two array may represent a rotation in a plane. Ensuring that the values stored in a variable are unitary assists the program developer. In conjunction with parameterized derived types, assertions can be enabled or disabled on a variable by variable basis. Estimated Impact: A new declaration must be supported in derived type definitions. Detailed Specification: The assertion will declare some property of instances of a derived type, so call it INVARIANT. An INVARIANT might be implemented as a premise or property of the assignment routine, or of default assignment. INVARIANT( ) This declaration applies to objects of the type when the assert level is set to do so. An is as defined for the ASSERT statement. Example: type :: rotation_t real, dimension( 2, 2) :: element contains procedure, pass :: norm => norm_2by2 invariant( abs( rotation_t% norm() - 1.0) < tol, & level= assert_warn) end type rotation_t Here, the type name refers to an instance of the type. Example using PDT: type :: rotation_t( alevel) integer, kind :: alevel real, dimension( 2, 2) :: element contains invariant( abs( norm( rotation_t) - 1.0) < tol, & level= alevel) end type rotation_t type( rotation_t( assert_ignore)) :: party_member type( rotation_t( assert_halt)) :: usual_suspect Assignments to party_member are not checked, assignments to usual_suspect are. Again, the type name refers to an instance of the type, in this case, it is the variable to which assignment is being made. History: