Program Defined Attributes

From wiki.visual-prolog.com

Revision as of 14:15, 13 February 2019 by Thomas Linder Puls (talk | contribs) (initial version)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

 


This section describes how to define attributes that can be written on entities in a program. Currently, the attributes will not have any semantics influence nor can they be accessed through reflection, so they will only be accessible to tools that parse the code itself.

Example As an example consider the use of the attributes meta and metaDisplayAs
interface person supports personGenerated
    [meta]
 
properties
    name : string [meta].
    address : string [metaDisplayAs("address")].
 
end interface person
The basic idea is that an external tool parses the file (in an external step) and based on the code and the meta attributes generates the interface (+ class + implementation) personGenerated. The exact contents of the generated class is not relevant for this example; the important thing is that the attributes are used to define what should be generated and how it should function.

Without any further action the compiler will generate warnings for the attributes above. To avoid this the attributes can be defined in a functor domain which is given the attribute attribute:

namespace ns
 
interface meta
 
domains
    meta =
        meta;
        metaDisplayAs(string DisplayAs) [attribute].
 
end interface meta

Notice that the attributes are both in a namespace and a scope.

Using normal rules that will handle name conflicts. On the other hand the person interface will use to qualified attributes:

interface person supports personGenerated
    [ns\meta::meta]
 
properties
    name : string [ns\meta::meta].
    address : string [ns\meta::metaDisplayAs("address")].
 
end interface person

The qualification can be omitted by opening the scope

interface person supports personGenerated
    open ns\meta
    [meta]
 
properties
    name : string [meta].
    address : string [metaDisplayAs("address")].
 
end interface person

Given this the compiler will:

  • suppress the w193 warning (for unknown attribute)
  • resolve ambiguities (when everybody want to use meta and test as attribute names)
  • type check the attributes

The attributes will have to follow the rules for (compile time computable) constants (i.e. cannot contain function and predicate calls).