Wiki
Version 3 (modified by hopscc, 12 years ago)

--

Attributes

Attributes are items of metadata appended to specific program items that end up supplied with your program.

Some of these are given as of standard (Types and Members).

User defined or Custom attributes can be declared and given to specify additional information required or desired.

These attributes provided can be then queried by standard or additional tools or by runtime examination (reflection).

Cobra provides for the declaration and use of attributes similarly to C# ( on .Net) and Java Language (on Jvm). These are declared using a keyword prefix (has) with the Attribute (or comma separated Attributes) following (similar in form to a cobra Type declaration) rather than a leading syntactic punctuated clause (C# square brackets or Java @named-annotation).

Minimally, Attributes can be provided on classes and methods, class Fields and properties.

Grammar

... has <Attribute>[, <Attribute> ...]

may be optionally appended to the declaration of a Type (Class, Struct, Interface, Mixin, Enum, Extension, Event, Signature ) or to a Types Field (vars), Methods and Properties to provide an Attribute or Attributes on that item.

They can also be given on a methods parameters or an Assembly declaration.

Examples

With respect to GTK

class NameTreeNode
  has TreeNode(listOnly=true)  # this is an attribute ( with args)

  var name is String
      has TreeNodeValue(column=0)

  def doSomething is protected, override has STAThread
    pass

In the following, A and B.oldMethod are marked obsolete with the System.Obsolete Attribute. (This is a .Net common attribute - in cobra it would read better if named 'IsObsolete' or 'ObsoleteMarkerAttribute')

class A has Obsolete("use class B")

    def Method
        pass

class B
    
    def oldMethod has Obsolete("use NewMethod", true)
        pass

    def newMethod
        pass

Platform

In .Net custom attributes can be defined by defining a class subclassed from System.Attribute. Attributes are named in Cobra as per declaration and use in C#.

For Java, (.Net) Attributes are known as Annotations. The cobra declaration uses the same prefix but uses (subclasses) from Java Annotation definitions.

See Also

 C# Attributes

 C# Custom Attributes

 C#/.Net Common Attributes