<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
<meta name="generator" content="AsciiDoc 8.6.9" />
<title>Tutorial for the Next Scripting Language</title>
<style type="text/css">
/* Shared CSS for AsciiDoc xhtml11 and html5 backends */

/* Default font. */
body {
  font-family: Georgia,serif;
}

/* Title font. */
h1, h2, h3, h4, h5, h6,
div.title, caption.title,
thead, p.table.header,
#toctitle,
#author, #revnumber, #revdate, #revremark,
#footer {
  font-family: Arial,Helvetica,sans-serif;
}

body {
  margin: 1em 5% 1em 5%;
}

a {
  color: blue;
  text-decoration: underline;
}
a:visited {
  color: fuchsia;
}

em {
  font-style: italic;
  color: navy;
}

strong {
  font-weight: bold;
  color: #083194;
}

h1, h2, h3, h4, h5, h6 {
  color: #527bbd;
  margin-top: 1.2em;
  margin-bottom: 0.5em;
  line-height: 1.3;
}

h1, h2, h3 {
  border-bottom: 2px solid silver;
}
h2 {
  padding-top: 0.5em;
}
h3 {
  float: left;
}
h3 + * {
  clear: left;
}
h5 {
  font-size: 1.0em;
}

div.sectionbody {
  margin-left: 0;
}

hr {
  border: 1px solid silver;
}

p {
  margin-top: 0.5em;
  margin-bottom: 0.5em;
}

ul, ol, li > p {
  margin-top: 0;
}
ul > li     { color: #aaa; }
ul > li > * { color: black; }

.monospaced, code, pre {
  font-family: "Courier New", Courier, monospace;
  font-size: inherit;
  color: navy;
  padding: 0;
  margin: 0;
}
pre {
  white-space: pre-wrap;
}

#author {
  color: #527bbd;
  font-weight: bold;
  font-size: 1.1em;
}
#email {
}
#revnumber, #revdate, #revremark {
}

#footer {
  font-size: small;
  border-top: 2px solid silver;
  padding-top: 0.5em;
  margin-top: 4.0em;
}
#footer-text {
  float: left;
  padding-bottom: 0.5em;
}
#footer-badges {
  float: right;
  padding-bottom: 0.5em;
}

#preamble {
  margin-top: 1.5em;
  margin-bottom: 1.5em;
}
div.imageblock, div.exampleblock, div.verseblock,
div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
div.admonitionblock {
  margin-top: 1.0em;
  margin-bottom: 1.5em;
}
div.admonitionblock {
  margin-top: 2.0em;
  margin-bottom: 2.0em;
  margin-right: 10%;
  color: #606060;
}

div.content { /* Block element content. */
  padding: 0;
}

/* Block element titles. */
div.title, caption.title {
  color: #527bbd;
  font-weight: bold;
  text-align: left;
  margin-top: 1.0em;
  margin-bottom: 0.5em;
}
div.title + * {
  margin-top: 0;
}

td div.title:first-child {
  margin-top: 0.0em;
}
div.content div.title:first-child {
  margin-top: 0.0em;
}
div.content + div.title {
  margin-top: 0.0em;
}

div.sidebarblock > div.content {
  background: #ffffee;
  border: 1px solid #dddddd;
  border-left: 4px solid #f0f0f0;
  padding: 0.5em;
}

div.listingblock > div.content {
  border: 1px solid #dddddd;
  border-left: 5px solid #f0f0f0;
  background: #f8f8f8;
  padding: 0.5em;
}

div.quoteblock, div.verseblock {
  padding-left: 1.0em;
  margin-left: 1.0em;
  margin-right: 10%;
  border-left: 5px solid #f0f0f0;
  color: #888;
}

div.quoteblock > div.attribution {
  padding-top: 0.5em;
  text-align: right;
}

div.verseblock > pre.content {
  font-family: inherit;
  font-size: inherit;
}
div.verseblock > div.attribution {
  padding-top: 0.75em;
  text-align: left;
}
/* DEPRECATED: Pre version 8.2.7 verse style literal block. */
div.verseblock + div.attribution {
  text-align: left;
}

div.admonitionblock .icon {
  vertical-align: top;
  font-size: 1.1em;
  font-weight: bold;
  text-decoration: underline;
  color: #527bbd;
  padding-right: 0.5em;
}
div.admonitionblock td.content {
  padding-left: 0.5em;
  border-left: 3px solid #dddddd;
}

div.exampleblock > div.content {
  border-left: 3px solid #dddddd;
  padding-left: 0.5em;
}

div.imageblock div.content { padding-left: 0; }
span.image img { border-style: none; vertical-align: text-bottom; }
a.image:visited { color: white; }

dl {
  margin-top: 0.8em;
  margin-bottom: 0.8em;
}
dt {
  margin-top: 0.5em;
  margin-bottom: 0;
  font-style: normal;
  color: navy;
}
dd > *:first-child {
  margin-top: 0.1em;
}

ul, ol {
    list-style-position: outside;
}
ol.arabic {
  list-style-type: decimal;
}
ol.loweralpha {
  list-style-type: lower-alpha;
}
ol.upperalpha {
  list-style-type: upper-alpha;
}
ol.lowerroman {
  list-style-type: lower-roman;
}
ol.upperroman {
  list-style-type: upper-roman;
}

div.compact ul, div.compact ol,
div.compact p, div.compact p,
div.compact div, div.compact div {
  margin-top: 0.1em;
  margin-bottom: 0.1em;
}

tfoot {
  font-weight: bold;
}
td > div.verse {
  white-space: pre;
}

div.hdlist {
  margin-top: 0.8em;
  margin-bottom: 0.8em;
}
div.hdlist tr {
  padding-bottom: 15px;
}
dt.hdlist1.strong, td.hdlist1.strong {
  font-weight: bold;
}
td.hdlist1 {
  vertical-align: top;
  font-style: normal;
  padding-right: 0.8em;
  color: navy;
}
td.hdlist2 {
  vertical-align: top;
}
div.hdlist.compact tr {
  margin: 0;
  padding-bottom: 0;
}

.comment {
  background: yellow;
}

.footnote, .footnoteref {
  font-size: 0.8em;
}

span.footnote, span.footnoteref {
  vertical-align: super;
}

#footnotes {
  margin: 20px 0 20px 0;
  padding: 7px 0 0 0;
}

#footnotes div.footnote {
  margin: 0 0 5px 0;
}

#footnotes hr {
  border: none;
  border-top: 1px solid silver;
  height: 1px;
  text-align: left;
  margin-left: 0;
  width: 20%;
  min-width: 100px;
}

div.colist td {
  padding-right: 0.5em;
  padding-bottom: 0.3em;
  vertical-align: top;
}
div.colist td img {
  margin-top: 0.3em;
}

@media print {
  #footer-badges { display: none; }
}

#toc {
  margin-bottom: 2.5em;
}

#toctitle {
  color: #527bbd;
  font-size: 1.1em;
  font-weight: bold;
  margin-top: 1.0em;
  margin-bottom: 0.1em;
}

div.toclevel0, div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
  margin-top: 0;
  margin-bottom: 0;
}
div.toclevel2 {
  margin-left: 2em;
  font-size: 0.9em;
}
div.toclevel3 {
  margin-left: 4em;
  font-size: 0.9em;
}
div.toclevel4 {
  margin-left: 6em;
  font-size: 0.9em;
}

span.aqua { color: aqua; }
span.black { color: black; }
span.blue { color: blue; }
span.fuchsia { color: fuchsia; }
span.gray { color: gray; }
span.green { color: green; }
span.lime { color: lime; }
span.maroon { color: maroon; }
span.navy { color: navy; }
span.olive { color: olive; }
span.purple { color: purple; }
span.red { color: red; }
span.silver { color: silver; }
span.teal { color: teal; }
span.white { color: white; }
span.yellow { color: yellow; }

span.aqua-background { background: aqua; }
span.black-background { background: black; }
span.blue-background { background: blue; }
span.fuchsia-background { background: fuchsia; }
span.gray-background { background: gray; }
span.green-background { background: green; }
span.lime-background { background: lime; }
span.maroon-background { background: maroon; }
span.navy-background { background: navy; }
span.olive-background { background: olive; }
span.purple-background { background: purple; }
span.red-background { background: red; }
span.silver-background { background: silver; }
span.teal-background { background: teal; }
span.white-background { background: white; }
span.yellow-background { background: yellow; }

span.big { font-size: 2em; }
span.small { font-size: 0.6em; }

span.underline { text-decoration: underline; }
span.overline { text-decoration: overline; }
span.line-through { text-decoration: line-through; }

div.unbreakable { page-break-inside: avoid; }


/*
 * xhtml11 specific
 *
 * */

div.tableblock {
  margin-top: 1.0em;
  margin-bottom: 1.5em;
}
div.tableblock > table {
  border: 3px solid #527bbd;
}
thead, p.table.header {
  font-weight: bold;
  color: #527bbd;
}
p.table {
  margin-top: 0;
}
/* Because the table frame attribute is overriden by CSS in most browsers. */
div.tableblock > table[frame="void"] {
  border-style: none;
}
div.tableblock > table[frame="hsides"] {
  border-left-style: none;
  border-right-style: none;
}
div.tableblock > table[frame="vsides"] {
  border-top-style: none;
  border-bottom-style: none;
}


/*
 * html5 specific
 *
 * */

table.tableblock {
  margin-top: 1.0em;
  margin-bottom: 1.5em;
}
thead, p.tableblock.header {
  font-weight: bold;
  color: #527bbd;
}
p.tableblock {
  margin-top: 0;
}
table.tableblock {
  border-width: 3px;
  border-spacing: 0px;
  border-style: solid;
  border-color: #527bbd;
  border-collapse: collapse;
}
th.tableblock, td.tableblock {
  border-width: 1px;
  padding: 4px;
  border-style: solid;
  border-color: #527bbd;
}

table.tableblock.frame-topbot {
  border-left-style: hidden;
  border-right-style: hidden;
}
table.tableblock.frame-sides {
  border-top-style: hidden;
  border-bottom-style: hidden;
}
table.tableblock.frame-none {
  border-style: hidden;
}

th.tableblock.halign-left, td.tableblock.halign-left {
  text-align: left;
}
th.tableblock.halign-center, td.tableblock.halign-center {
  text-align: center;
}
th.tableblock.halign-right, td.tableblock.halign-right {
  text-align: right;
}

th.tableblock.valign-top, td.tableblock.valign-top {
  vertical-align: top;
}
th.tableblock.valign-middle, td.tableblock.valign-middle {
  vertical-align: middle;
}
th.tableblock.valign-bottom, td.tableblock.valign-bottom {
  vertical-align: bottom;
}


/*
 * manpage specific
 *
 * */

body.manpage h1 {
  padding-top: 0.5em;
  padding-bottom: 0.5em;
  border-top: 2px solid silver;
  border-bottom: 2px solid silver;
}
body.manpage h2 {
  border-style: none;
}
body.manpage div.sectionbody {
  margin-left: 3em;
}

@media print {
  body.manpage div#toc { display: none; }
}


</style>
<script type="text/javascript">
/*<![CDATA[*/
var asciidoc = {  // Namespace.

/////////////////////////////////////////////////////////////////////
// Table Of Contents generator
/////////////////////////////////////////////////////////////////////

/* Author: Mihai Bazon, September 2002
 * http://students.infoiasi.ro/~mishoo
 *
 * Table Of Content generator
 * Version: 0.4
 *
 * Feel free to use this script under the terms of the GNU General Public
 * License, as long as you do not remove or alter this notice.
 */

 /* modified by Troy D. Hanson, September 2006. License: GPL */
 /* modified by Stuart Rackham, 2006, 2009. License: GPL */

// toclevels = 1..4.
toc: function (toclevels) {

  function getText(el) {
    var text = "";
    for (var i = el.firstChild; i != null; i = i.nextSibling) {
      if (i.nodeType == 3 /* Node.TEXT_NODE */) // IE doesn't speak constants.
        text += i.data;
      else if (i.firstChild != null)
        text += getText(i);
    }
    return text;
  }

  function TocEntry(el, text, toclevel) {
    this.element = el;
    this.text = text;
    this.toclevel = toclevel;
  }

  function tocEntries(el, toclevels) {
    var result = new Array;
    var re = new RegExp('[hH]([1-'+(toclevels+1)+'])');
    // Function that scans the DOM tree for header elements (the DOM2
    // nodeIterator API would be a better technique but not supported by all
    // browsers).
    var iterate = function (el) {
      for (var i = el.firstChild; i != null; i = i.nextSibling) {
        if (i.nodeType == 1 /* Node.ELEMENT_NODE */) {
          var mo = re.exec(i.tagName);
          if (mo && (i.getAttribute("class") || i.getAttribute("className")) != "float") {
            result[result.length] = new TocEntry(i, getText(i), mo[1]-1);
          }
          iterate(i);
        }
      }
    }
    iterate(el);
    return result;
  }

  var toc = document.getElementById("toc");
  if (!toc) {
    return;
  }

  // Delete existing TOC entries in case we're reloading the TOC.
  var tocEntriesToRemove = [];
  var i;
  for (i = 0; i < toc.childNodes.length; i++) {
    var entry = toc.childNodes[i];
    if (entry.nodeName.toLowerCase() == 'div'
     && entry.getAttribute("class")
     && entry.getAttribute("class").match(/^toclevel/))
      tocEntriesToRemove.push(entry);
  }
  for (i = 0; i < tocEntriesToRemove.length; i++) {
    toc.removeChild(tocEntriesToRemove[i]);
  }

  // Rebuild TOC entries.
  var entries = tocEntries(document.getElementById("content"), toclevels);
  for (var i = 0; i < entries.length; ++i) {
    var entry = entries[i];
    if (entry.element.id == "")
      entry.element.id = "_toc_" + i;
    var a = document.createElement("a");
    a.href = "#" + entry.element.id;
    a.appendChild(document.createTextNode(entry.text));
    var div = document.createElement("div");
    div.appendChild(a);
    div.className = "toclevel" + entry.toclevel;
    toc.appendChild(div);
  }
  if (entries.length == 0)
    toc.parentNode.removeChild(toc);
},


/////////////////////////////////////////////////////////////////////
// Footnotes generator
/////////////////////////////////////////////////////////////////////

/* Based on footnote generation code from:
 * http://www.brandspankingnew.net/archive/2005/07/format_footnote.html
 */

footnotes: function () {
  // Delete existing footnote entries in case we're reloading the footnodes.
  var i;
  var noteholder = document.getElementById("footnotes");
  if (!noteholder) {
    return;
  }
  var entriesToRemove = [];
  for (i = 0; i < noteholder.childNodes.length; i++) {
    var entry = noteholder.childNodes[i];
    if (entry.nodeName.toLowerCase() == 'div' && entry.getAttribute("class") == "footnote")
      entriesToRemove.push(entry);
  }
  for (i = 0; i < entriesToRemove.length; i++) {
    noteholder.removeChild(entriesToRemove[i]);
  }

  // Rebuild footnote entries.
  var cont = document.getElementById("content");
  var spans = cont.getElementsByTagName("span");
  var refs = {};
  var n = 0;
  for (i=0; i<spans.length; i++) {
    if (spans[i].className == "footnote") {
      n++;
      var note = spans[i].getAttribute("data-note");
      if (!note) {
        // Use [\s\S] in place of . so multi-line matches work.
        // Because JavaScript has no s (dotall) regex flag.
        note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1];
        spans[i].innerHTML =
          "[<a id='_footnoteref_" + n + "' href='#_footnote_" + n +
          "' title='View footnote' class='footnote'>" + n + "</a>]";
        spans[i].setAttribute("data-note", note);
      }
      noteholder.innerHTML +=
        "<div class='footnote' id='_footnote_" + n + "'>" +
        "<a href='#_footnoteref_" + n + "' title='Return to text'>" +
        n + "</a>. " + note + "</div>";
      var id =spans[i].getAttribute("id");
      if (id != null) refs["#"+id] = n;
    }
  }
  if (n == 0)
    noteholder.parentNode.removeChild(noteholder);
  else {
    // Process footnoterefs.
    for (i=0; i<spans.length; i++) {
      if (spans[i].className == "footnoteref") {
        var href = spans[i].getElementsByTagName("a")[0].getAttribute("href");
        href = href.match(/#.*/)[0];  // Because IE return full URL.
        n = refs[href];
        spans[i].innerHTML =
          "[<a href='#_footnote_" + n +
          "' title='View footnote' class='footnote'>" + n + "</a>]";
      }
    }
  }
},

install: function(toclevels) {
  var timerId;

  function reinstall() {
    asciidoc.footnotes();
    if (toclevels) {
      asciidoc.toc(toclevels);
    }
  }

  function reinstallAndRemoveTimer() {
    clearInterval(timerId);
    reinstall();
  }

  timerId = setInterval(reinstall, 500);
  if (document.addEventListener)
    document.addEventListener("DOMContentLoaded", reinstallAndRemoveTimer, false);
  else
    window.onload = reinstallAndRemoveTimer;
}

}
asciidoc.install(4);
/*]]>*/
</script>
</head>
<body class="article">
<div id="header">
<h1>Tutorial for the Next Scripting Language</h1>
<span id="author">Gustaf Neumann &lt;neumann@wu-wien.ac.at&gt;, Stefan Sobernig &lt;stefan.sobernig@wu.ac.at&gt;</span><br />
<span id="revnumber">version 2.1.0,</span>
<span id="revdate">December 2016</span>
<br /><span id="revremark">Written for the Initial Release of the Next Scripting Framework.</span>
<div id="toc">
  <div id="toctitle">Table of Contents</div>
  <noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
</div>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="sidebarblock">
<div class="content">
<div class="title">Abstract</div>
<div class="paragraph"><p>This document provides a tutorial for the Next Scripting
Language NX.</p></div>
</div></div>
<div class="paragraph"><p>The Next Scripting Language (NX) is a highly flexible object oriented
scripting language based on Tcl <a href="#Ousterhout 1990">[Ousterhout 1990]</a>. NX is a successor
of XOTcl 1 <a href="#Neumann and Zdun 2000a">[Neumann and Zdun 2000a]</a> and was developed based on 10
years of experience with XOTcl in projects containing several hundred
thousand lines of code. While XOTcl was the first language designed to
provide <em>language support for design patterns</em>, the focus of the Next
Scripting Framework and NX is on combining this with <em>Language
Oriented Programming</em>. In many respects, NX was designed to ease the
learning of the language for novices (by using a more mainstream
terminology, higher orthogonality of the methods, less predefined
methods), to improve maintainability (remove sources of common errors)
and to encourage developers to write better structured programs (to
provide interfaces) especially for large projects, where many
developers are involved.</p></div>
<div class="paragraph"><p>The Next Scripting Language is based on the Next Scripting Framework
(NSF) which was developed based on the notion of language oriented
programming. The Next Scripting Frameworks provides C-level support
for defining and hosting multiple object systems in a single Tcl
interpreter.  The name of the Next Scripting Framework is derived from
the universal method combinator "next", which was introduced in XOTcl.
The combinator "next" serves as a single instrument for method
combination with filters, per-object and transitive per-class mixin
classes, object methods and multiple inheritance.</p></div>
<div class="paragraph"><p>The definition of NX is fully scripted (e.g.  defined in
<code>nx.tcl</code>). The Next Scripting Framework is shipped with three language
definitions, containing NX and XOTcl 2. Most of the existing XOTcl 1
programs can be used without modification in the Next Scripting
Framework by using XOTcl 2. The Next Scripting Framework requires Tcl
8.5 or newer.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_nx_and_its_roots">1. NX and its Roots</h2>
<div class="sectionbody">
<div class="paragraph"><p>Object oriented extensions of Tcl have quite a
long history. Two of the most prominent early Tcl based OO languages
were <em>incr Tcl</em> (abbreviated as itcl) and Object Tcl (<em>OTcl</em>
<a href="#Wetherall and Lindblad 1995">[Wetherall and Lindblad 1995]</a>). While itcl provides a traditional
C++/Java-like object system, OTcl was following the CLOS approach and
supports a dynamic object system, allowing incremental class and
object extensions and re-classing of objects.</p></div>
<div class="paragraph"><p>Extended Object Tcl (abbreviated as XOTcl <a href="#Neumann and Zdun 2000a">[Neumann and Zdun 2000a]</a>)
is a successor of OTcl and was the first language providing language
support for design patterns. XOTcl extends OTcl by providing namespace
support, adding assertions, dynamic object aggregations, slots and by
introducing per-object and per-class filters and per-object and
per-class mixins.</p></div>
<div class="paragraph"><p>XOTcl was so far released in more than 30 versions. It is described in
its detail in more than 20 papers and serves as a basis for other
object systems like TclOO [Donal ???]. The scripting language <em>NX</em> and
the <em>Next Scripting Framework</em> <a href="#Neumann and Sobernig 2009">[Neumann and Sobernig 2009]</a> extend
the basic ideas of XOTcl by providing support for <em>language-oriented
programming</em>. The the Next Scripting Framework supports multiple
object systems concurrently. Effectively, every object system has
different base classes for creating objects and classes. Therefore,
these object systems can have different interfaces and can
follow different naming conventions for built-in methods. Currently,
the Next Scripting Framework is packaged with three object systems:
NX, XOTcl 2.0, and TclCool (the language introduced by TIP#279).</p></div>
<div class="imageblock" style="text-align:center;">
<div class="content">
<img src="languages.png" alt="Languages" width="500" />
</div>
<div class="title">Figure 1. Language History of the Next Scripting Language</div>
</div>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p>The primary purpose of this document is to introduce NX to beginners.
We expect some prior knowledge of programming languages, and some
knowledge about Tcl. In the following sections we introduce NX by
examples. In later sections we introduce the more advanced concepts of
the language. Conceptually, most of the addressed concepts are very
similar to XOTcl. Concerning the differences between NX and XOTcl,
please refer to the <em>Migration Guide for the Next Scripting Language</em>.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_introductory_overview_example_stack">2. Introductory Overview Example: Stack</h2>
<div class="sectionbody">
<div class="paragraph"><p>A classical programming example is the implementation of a stack, which
is most likely familiar to many readers from many introductory
programming courses. A stack is a last-in first-out data structure
which is manipulated via operations like <code>push</code> (add something to the
stack) and <code>pop</code> remove an entry from the stack. These operations are
called <em>methods</em> in the context of object oriented programming
systems. Primary goals of object orientation are encapsulation and
abstraction. Therefore, we define a common unit (a class) that defines
and encapsulates the behavior of a stack and provides methods to a user
of the data structure that abstract from the actual implementation.</p></div>
<div class="sect2">
<h3 id="_define_a_class_stack">2.1. Define a Class "Stack"</h3>
<div class="paragraph"><p>In our first example, we define a class named <code>Stack</code> with the methods
<code>push</code> and <code>pop</code>. When an instance of the stack is created (e.g. a
concrete stack <code>s1</code>) the stack will contain an instance variable named
<code>things</code>, initialized with the an empty list.</p></div>
<div class="paragraph" id="xmp-class-stack"><div class="title">Listing 2: Class Stack</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Stack {

   <span class='nx-[namespace tail [nx::self]]'>#
</span>   <span class='nx-[namespace tail [nx::self]]'># Stack of Things
</span>   <span class='nx-[namespace tail [nx::self]]'>#
</span>
   <span class='nx-keyword'>:variable</span> things {}

   <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> push {thing} {
      <span class='nx-keyword'>set</span> :things [<span class='nx-keyword'>linsert</span> <span class='nx-[namespace tail [nx::self]]'>${</span><span class='nx-variable'>:things}</span> 0 <span class='nx-[namespace tail [nx::self]]'>$thing</span>]
      <span class='nx-keyword'>return</span> <span class='nx-[namespace tail [nx::self]]'>$thing</span>
   }

   <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> pop {} {
      <span class='nx-keyword'>set</span> top [<span class='nx-keyword'>lindex</span> <span class='nx-[namespace tail [nx::self]]'>${</span><span class='nx-variable'>:things}</span> 0]
      <span class='nx-keyword'>set</span> :things [<span class='nx-keyword'>lrange</span> <span class='nx-[namespace tail [nx::self]]'>${</span><span class='nx-variable'>:things}</span> 1 end]
      <span class='nx-keyword'>return</span> <span class='nx-[namespace tail [nx::self]]'>$top</span>
   }
}</pre></td></tr></table></div></div>
<div class="paragraph"><p>Typically, classes are defined in NX via <code>nx::Class create</code>, followed
by the name of the new class (here: <code>Stack</code>). The definition of the
stack placed between curly braces and contains here just the method
definitions. Methods of the class are defined via <code>:method</code> followed
by the name of the method, an argument list and the body of the
method, consisting of Tcl and NX statements.</p></div>
<div class="paragraph"><p>When an instance of <code>Stack</code> is created, it will contain an instance
variable named <code>things</code>. If several <code>Stack</code> instances are created,
each of the instances will have their own (same-named but different)
instance variable. The instance variable <code>things</code> is used in our
example as a list for the internal representation of the stack. We
define in a next step the methods to access and modify this list
structure. A user of the stack using the provided methods does not
have to have any knowledge about the name or the structure of the
internal representation (the instance variable <code>things</code>).</p></div>
<div class="paragraph"><p>The method <code>push</code> receives an argument <code>thing</code> which should be placed
on the stack. Note that we do not have to specify the type of the
element on the stack, so we can push strings as well as numbers or
other kind of things. When an element is pushed, we add this element
as the first element to the list <code>things</code>. We insert the element using
the Tcl command <code>linsert</code> which receives the list as first element,
the position where the element should be added as second and the new
element as third argument. To access the value of the instance
variable we use Tcl&#8217;s dollar operator followed by the name. The
names of instance variables are preceded with a colon <code>:</code>. Since the
name contains a non-plain character, Tcl requires us to put braces
around the name. The command <code>linsert</code> and its arguments are placed
between square brackets. This means that the function <code>linsert</code> is called and
a new list is returned, where the new element is inserted at the first
position (index 0) in the list <code>things</code>. The result of the <code>linsert</code>
function is assigned again to the instance variable <code>things</code>, which is
updated this way.  Finally the method <code>push</code> returns the pushed thing
using the <code>return</code> statement.</p></div>
<div class="paragraph"><p>The method <code>pop</code> returns the most recently stacked element and removes
it from the stack. Therefore, it takes the first element from the list
(using the Tcl command <code>lindex</code>), assigns it to the method-scoped
variable <code>top</code>, removes the element from the instance variable
<code>things</code> (by using the Tcl command <code>lrange</code>) and returns the value
popped element <code>top</code>.</p></div>
<div class="paragraph"><p>This finishes our first implementation of the stack, more enhanced
versions will follow. Note that the methods <code>push</code> and <code>pop</code> are
defined as <code>public</code>; this means that these methods can be
used from all other objects in the system. Therefore, these methods
provide an interface to the stack implementation.</p></div>
<div class="paragraph" id="xmp-using-stack"><div class="title">Listing 3: Using the Stack</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-[namespace tail [nx::self]]'>#!/usr/bin/env tclsh
</span><span class='nx-keyword'>package</span> <span class='nx-keyword'>require</span> nx

<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Stack {

   <span class='nx-[namespace tail [nx::self]]'>#
</span>   <span class='nx-[namespace tail [nx::self]]'># Stack of Things
</span>   <span class='nx-[namespace tail [nx::self]]'>#
</span>   ....
}

Stack <span class='nx-keyword'>create</span> s1
s1 push a
s1 push b
s1 push c
<span class='nx-keyword'>puts</span> [s1 pop]
<span class='nx-keyword'>puts</span> [s1 pop]
s1 <span class='nx-keyword'>destroy</span></pre></td></tr></table></div></div>
<div class="paragraph"><p>Now we want to use the stack. The code snippet in <a href="#xmp-using-stack">Listing 3</a> shows how to use the class Stack in a script.
Since NX is based on Tcl, the script will be called with the Tcl shell
<code>tclsh</code>. In the Tcl shell we have to <code>require package nx</code> to use the
Next Scripting Framework and NX. The next lines contain the definition
of the stack as presented before. Of course, it is as well possible to
make the definition of the stack an own package, such we could simple
say <code>package require stack</code>, or to save the definition of a stack
simply in a file and load it via <code>source</code>.</p></div>
<div class="paragraph"><p>In line 12 we create an instance of the stack, namely the stack object
<code>s1</code>. The object <code>s1</code> is an instance of <code>Stack</code> and has therefore
access to its methods. The methods like <code>push</code> or <code>pop</code> can be invoked
via a command starting with the object name followed by the
method name. In lines 13-15 we push on the stack the values <code>a</code>, then
<code>b</code>, and <code>c</code>. In line 16 we output the result of the <code>pop</code> method
using the Tcl command <code>puts</code>. We will see on standard output the
value+c+ (the last stacked item). The output of the line 17 is the
value <code>b</code> (the previously stacked item). Finally, in line 18 we
destroy the object. This is not necessary here, but shows the life
cycle of an object. In some respects, <code>destroy</code> is the counterpart of
<code>create</code> from line 12.</p></div>
<div class="imageblock" id="fig-class-object" style="text-align:center;">
<div class="content">
<img src="object-class-appclass.png" alt="object-class-appclass.png" />
</div>
<div class="title">Figure 4. Class and Object Diagram</div>
</div>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p><a href="#fig-class-object">Figure 4</a> shows the actual class and
object structure of the first <code>Stack</code> example. Note that the common
root class is <code>nx::Object</code> that contains methods for all objects.
Since classes are as well objects in NX, <code>nx::Class</code> is a
specialization of <code>nx::Object</code>. <code>nx::Class</code> provides methods for
creating objects, such as the method <code>create</code> which is used to create
objects (and classes as well).</p></div>
</div>
<div class="sect2">
<h3 id="_define_an_object_named_stack">2.2. Define an Object Named "stack"</h3>
<div class="paragraph"><p>The definition of the stack in <a href="#xmp-class-stack">Listing 2</a>
follows the traditional object oriented approach, found in
practically every object oriented programming language: Define a class
with some methods, create instances from this class, and use the
methods defined in the class in the instances of the class.</p></div>
<div class="paragraph"><p>In our next example, we introduce <em>generic objects</em> and <em>object
specific methods</em>. With NX, we can define generic objects, which are
instances of the most generic class <code>nx::Object</code> (sometimes called
<em>common root class</em>). <code>nx::Object</code> is predefined and contains a
minimal set of methods applicable to all NX objects.  In this example,
we define a generic object named <code>stack</code> and provide methods for this
object. The methods defined above were methods provided by a class for
objects. Now we define object specific methods, which are methods
applicable only to the object for which they are defined.</p></div>
<div class="paragraph" id="xmp-object-stack"><div class="title">Listing 5: Object stack</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Object</span> <span class='nx-keyword'>create</span> stack {

   <span class='nx-keyword'>:object</span> <span class='nx-keyword'>variable</span> things {}

   <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> push {thing} {
      <span class='nx-keyword'>set</span> :things [<span class='nx-keyword'>linsert</span> <span class='nx-[namespace tail [nx::self]]'>${</span><span class='nx-variable'>:things}</span> 0 <span class='nx-[namespace tail [nx::self]]'>$thing</span>]
      <span class='nx-keyword'>return</span> <span class='nx-[namespace tail [nx::self]]'>$thing</span>
   }

   <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> pop {} {
      <span class='nx-keyword'>set</span> top [<span class='nx-keyword'>lindex</span> <span class='nx-[namespace tail [nx::self]]'>${</span><span class='nx-variable'>:things}</span> 0]
      <span class='nx-keyword'>set</span> :things [<span class='nx-keyword'>lrange</span> <span class='nx-[namespace tail [nx::self]]'>${</span><span class='nx-variable'>:things}</span> 1 end]
      <span class='nx-keyword'>return</span> <span class='nx-[namespace tail [nx::self]]'>$top</span>
   }
}</pre></td></tr></table></div></div>
<div class="paragraph"><p>The example in <a href="#xmp-object-stack">Listing 5</a> defines the
object <code>stack</code> in a very similar way as the class <code>Stack</code>. But the
following points are different.</p></div>
<div class="ulist"><ul>
<li>
<p>
First, we use <code>nx::Object</code> instead of <code>nx::Class</code> to denote
  that we want to create a generic object, not a class.
</p>
</li>
<li>
<p>
We use <code>:object variable</code> to define the variable <code>things</code>  just for
   this single instance  (the object <code>stack</code>).
</p>
</li>
<li>
<p>
The definition for the methods <code>push</code> and <code>pop</code> are the same as
  before, but here we defined these with <code>object method</code>. Therefore,
  these two methods <code>push</code> and <code>pop</code> are object-specific.
</p>
</li>
</ul></div>
<div class="paragraph"><p>In order to use
the stack, we can use directly the object <code>stack</code> in the same way as
we have used the object <code>s1</code> in <a href="#xmp-using-stack">Listing 3</a>
the class diagram for this the object <code>stack</code>.</p></div>
<div class="imageblock" id="img-object-stack" style="text-align:center;">
<div class="content">
<img src="object-stack.png" alt="object-stack.png" />
</div>
<div class="title">Figure 6. Object stack</div>
</div>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p>A reader might wonder when to use a class <code>Stack</code> or rather an object
<code>stack</code>. A big difference is certainly that one can define easily
multiple instances of a class, while the object is actually a
single, tailored entity. The concept of the object <code>stack</code> is similar to a module,
providing a certain functionality via a common interface, without
providing the functionality to create multiple instances. The reuse of
methods provided by the class to objects is as well a difference. If
the methods of the class are updated, all instances of the class will
immediately get the modified behavior. However, this does not mean that
the reuse for the methods of <code>stack</code> is not possible. NX allows for
example to copy objects (similar to prototype based languages) or to
reuse methods via e.g. aliases (more about this later).</p></div>
<div class="paragraph"><p>Note that we use capitalized names for classes and lowercase names for
instances. This is not required and a pure convention making it easier
to understand scripts without much analysis.</p></div>
</div>
<div class="sect2">
<h3 id="_implementing_features_using_mixin_classes">2.3. Implementing Features using Mixin Classes</h3>
<div class="paragraph"><p>So far, the definition of the stack methods was pretty minimal.
Suppose, we want to define "safe stacks" that protect e.g. against
stack under-runs (a stack under-run happens, when more <code>pop</code> than
<code>push</code> operations are issued on a stack). Safety checking can be
implemented mostly independent from the implementation details of the
stack (usage of internal data structures). There are as well different
ways of checking the safety. Therefore we say that safety checking is
orthogonal to the stack core implementation.</p></div>
<div class="paragraph"><p>With NX we can define stack-safety as a separate class using methods
with the same names as the implementations before, and "mix" this
behavior into classes or objects. The implementation of <code>Safety</code> in
stack under-runs and to issue error messages, when this happens.</p></div>
<div class="paragraph" id="xmp-class-safety"><div class="title">Listing 7: Class Safety</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Safety {

  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-[namespace tail [nx::self]]'># Implement stack safety by defining an additional
</span>  <span class='nx-[namespace tail [nx::self]]'># instance variable named "count" that keeps track of
</span>  <span class='nx-[namespace tail [nx::self]]'># the number of stacked elements. The methods of
</span>  <span class='nx-[namespace tail [nx::self]]'># this class have the same names and argument lists
</span>  <span class='nx-[namespace tail [nx::self]]'># as the methods of Stack; these methods "shadow"
</span>  <span class='nx-[namespace tail [nx::self]]'># the methods of class Stack.
</span>  <span class='nx-[namespace tail [nx::self]]'>#
</span>
  <span class='nx-keyword'>:variable</span> count 0

  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> push {thing} {
    <span class='nx-keyword'>incr</span> :count
    <span class='nx-keyword'>next</span>
  }

  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> pop {} {
    <span class='nx-keyword'>if</span> {<span class='nx-[namespace tail [nx::self]]'>${</span><span class='nx-variable'>:count}</span> == 0} then { <span class='nx-keyword'>error</span> <span class='nx-string'>"Stack empty!"</span> }
    <span class='nx-keyword'>incr</span> :count -1
    <span class='nx-keyword'>next</span>
  }
}</pre></td></tr></table></div></div>
<div class="paragraph"><p>Note that all the methods of the class <code>Safety</code> end with <code>next</code>.
This command is a primitive command of NX, which calls the
same-named method with the same argument list as the current
invocation.</p></div>
<div class="paragraph"><p>Assume we save the definition of the class <code>Stack</code> in a file named
<code>Stack.tcl</code> and the definition of the class <code>Safety</code> in a file named
<code>Safety.tcl</code> in the current directory. When we load the classes
<code>Stack</code> and <code>Safety</code> into the same script (see the terminal dialog in
e.g. a certain stack <code>s2</code> as a safe stack, while all other stacks
(such as <code>s1</code>) might be still "unsafe". This can be achieved via the
option <code>-mixin</code> at the object creation time (see line 9 in
option <code>-mixin</code> mixes the class <code>Safety</code> into the new instance <code>s2</code>.</p></div>
<div class="paragraph" id="xmp-using-class-safety"><div class="title">Listing 8: Using the Class Safety</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
</pre></td><td class='nx-body'><pre class='nx'>% <span class='nx-keyword'>package</span> <span class='nx-keyword'>require</span> nx
2.0
% <span class='nx-keyword'>source</span> Stack.tcl
::Stack
% <span class='nx-keyword'>source</span> Safety.tcl
::Safety
% Stack <span class='nx-keyword'>create</span> s1
::s1
% Stack <span class='nx-keyword'>create</span> s2 -object-mixin Safety
::s2
% s2 push a
a
% s2 pop
a
% s2 pop
Stack empty!

% s1 <span class='nx-keyword'>info</span> precedence
::Stack ::nx::Object

% s2 <span class='nx-keyword'>info</span> precedence
::Safety ::Stack ::nx::Object</pre></td></tr></table></div></div>
<div class="paragraph"><p>When the method <code>push</code> of <code>s2</code> is called, first the method of the
mixin class <code>Safety</code> will be invoked that increments the counter and
continues with <code>next</code> to call the shadowed method, here the method
<code>push</code> of the <code>Stack</code> implementation that actually pushes the item.
The same happens, when <code>s2 pop</code> is invoked, first the method of
<code>Safety</code> is called, then the method of the <code>Stack</code>. When the stack is
empty (the value of <code>count</code> reaches 0), and <code>pop</code> is invoked, the
mixin class <code>Safety</code> generates an error message (raises an exception),
and does not invoke the method of the <code>Stack</code>.</p></div>
<div class="paragraph"><p>The last two commands in
<a href="#xmp-using-class-safety">Listing 8</a>
use introspection to query for the objects
<code>s1</code> and <code>s2</code> in which order the involved classes are processed. This
order is called the <code>precedence order</code> and is obtained via <code>info
precedence</code>. We see that the mixin class <code>Safety</code> is only in use for
<code>s2</code>, and takes there precedence over <code>Stack</code>. The common root class
<code>nx::Object</code> is for both <code>s1</code> and <code>s2</code> the base class.</p></div>
<div class="imageblock" id="img-per-object-mixin" style="text-align:center;">
<div class="content">
<img src="per-object-mixin.png" alt="per-object-mixin.png" />
</div>
<div class="title">Figure 9. Per-object Mixin</div>
</div>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p>Note that in <a href="#xmp-using-class-safety">Listing 8</a>,
the class <code>Safety</code> is only mixed into a single object (here
<code>s2</code>), therefore we refer to this case as a <em>per-object mixin</em>.
<a href="#img-per-object-mixin">Figure 9</a> shows the class
diagram, where the class <code>Safety</code> is used as a per-object mixin for
<code>s2</code>.</p></div>
<div class="paragraph"><p>The mixin class <code>Safety</code> can be used as well in other ways, such as e.g. for
defining classes of safe stacks:</p></div>
<div class="paragraph" id="xmp-class-safestack"><div class="title">Listing 10: Class SafeStack</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># Create a safe stack class by using Stack and mixin
</span><span class='nx-[namespace tail [nx::self]]'># Safety
</span><span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> SafeStack -superclass Stack -mixin Safety

SafeStack <span class='nx-keyword'>create</span> s3</pre></td></tr></table></div></div>
<div class="paragraph"><p>The difference of a per-class mixin and an per-object mixin is that
the per-class mixin is applicable to all instances of the
class. Therefore, we call these mixins also sometimes instance mixins.
In our example in <a href="#xmp-class-safestack">Listing 10</a>,
<code>Safety</code> is mixed into the definition of
<code>SafeStack</code>. Therefore, all instances of the class <code>SafeStack</code> (here
the instance <code>s3</code>) will be using the safety definitions.</p></div>
<div class="imageblock" id="img-per-class-mixin" style="text-align:center;">
<div class="content">
<img src="per-class-mixin.png" alt="per-class-mixin.png" />
</div>
<div class="title">Figure 11. Per-class Mixin</div>
</div>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p><a href="#img-per-class-mixin">Figure 11</a> shows the class diagram
for this definition.
Note that we could use <code>Safety</code> as well as a per-class mixin on
<code>Stack</code>. In this case, all stacks would be safe stacks and we could
not provide a selective feature selection (which might be perfectly
fine).</p></div>
</div>
<div class="sect2">
<h3 id="_define_different_kinds_of_stacks">2.4. Define Different Kinds of Stacks</h3>
<div class="paragraph"><p>The definition of <code>Stack</code> is generic and allows all kind of elements
to be stacked. Suppose, we want to use the generic stack definition,
but a certain stack (say, stack <code>s4</code>) should be a stack for integers
only. This behavior can be achieved by the same means as introduced
already in <a href="#xmp-object-stack">Listing 5</a>, namely
object-specific methods.</p></div>
<div class="paragraph" id="xmp-object-integer-stack"><div class="title">Listing 12: Object Integer Stack</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
</pre></td><td class='nx-body'><pre class='nx'>Stack <span class='nx-keyword'>create</span> s4 {

  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-[namespace tail [nx::self]]'># Create a stack with a object-specific method
</span>  <span class='nx-[namespace tail [nx::self]]'># to check the type of entries
</span>  <span class='nx-[namespace tail [nx::self]]'>#
</span>
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> push {thing:integer} {
    <span class='nx-keyword'>next</span>
  }
}</pre></td></tr></table></div></div>
<div class="paragraph"><p>The program snippet in <a href="#xmp-object-integer-stack">Listing 12</a> defines an instance <code>s4</code> of the class
<code>Stack</code> and provides an object specific method for <code>push</code> to implement
an integer stack. The method <code>pull</code> is the same for the integer stack
as for all other stacks, so it will be reused as usual from the class
<code>Stack</code>. The object-specific method <code>push</code> of <code>s4</code> has a value
constraint in its argument list (<code>thing:integer</code>) that makes sure,
that only integers can be stacked. In case the argument is not an
integer, an exception will be raised. Of course, one could perform the
value constraint checking as well in the body of the method <code>proc</code> by
accepting an generic argument and by performing the test for the value
in the body of the method. In the case, the passed value is an
integer, the <code>push</code> method of <a href="#xmp-object-integer-stack">Listing 12</a> calls <code>next</code>, and therefore calls the
shadowed generic definition of <code>push</code> as provided by <code>Stack</code>.</p></div>
<div class="paragraph" id="xmp-class-integer-stack"><div class="title">Listing 13: Class IntegerStack</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> IntegerStack -superclass Stack {

  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-[namespace tail [nx::self]]'># Create a Stack accepting only integers
</span>  <span class='nx-[namespace tail [nx::self]]'>#
</span>
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> push {thing:integer} {
    <span class='nx-keyword'>next</span>
  }
}</pre></td></tr></table></div></div>
<div class="paragraph"><p>An alternative approach is shown in
<a href="#xmp-class-integer-stack">Listing 13</a>, where the class
<code>IntegerStack</code> is defined, using the same method definition
as <code>s4</code>, this time on the class level.</p></div>
</div>
<div class="sect2">
<h3 id="_define_object_specific_methods_on_classes">2.5. Define Object Specific Methods on Classes</h3>
<div class="paragraph"><p>In our previous examples we defined methods provided by classes
(applicable for their instances) and object-specific methods (methods
defined on objects, which are only applicable for these objects). In
this section, we introduce methods that are defined on the class
objects. Such methods are sometimes called <em>class methods</em> or
<em>static methods</em>.</p></div>
<div class="paragraph"><p>In NX classes are objects, they are specialized objects with
additional methods. Methods for classes are often used for managing
the life-cycles of the instances of the classes (we will come to this
point in later sections in more detail). Since classes are objects, we
can use exactly the same notation as above to define class methods by
using <code>object method</code>. The methods defined on the class object are
in all respects identical with object specific methods shown in the
examples above.</p></div>
<div class="paragraph" id="xmp-stack2"><div class="title">Listing 14: Class Stack2</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Stack2 {

   <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> available_stacks {} {
      <span class='nx-keyword'>return</span> [<span class='nx-keyword'>llength</span> [<span class='nx-keyword'>:info</span> instances]]
   }

   <span class='nx-keyword'>:variable</span> things {}

   <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> push {thing} {
      <span class='nx-keyword'>set</span> :things [<span class='nx-keyword'>linsert</span> <span class='nx-[namespace tail [nx::self]]'>${</span><span class='nx-variable'>:things}</span> 0 <span class='nx-[namespace tail [nx::self]]'>$thing</span>]
      <span class='nx-keyword'>return</span> <span class='nx-[namespace tail [nx::self]]'>$thing</span>
   }

   <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> pop {} {
      <span class='nx-keyword'>set</span> top [<span class='nx-keyword'>lindex</span> <span class='nx-[namespace tail [nx::self]]'>${</span><span class='nx-variable'>:things}</span> 0]
      <span class='nx-keyword'>set</span> :things [<span class='nx-keyword'>lrange</span> <span class='nx-[namespace tail [nx::self]]'>${</span><span class='nx-variable'>:things}</span> 1 end]
      <span class='nx-keyword'>return</span> <span class='nx-[namespace tail [nx::self]]'>$top</span>
   }
}

Stack2 <span class='nx-keyword'>create</span> s1
Stack2 <span class='nx-keyword'>create</span> s2

<span class='nx-keyword'>puts</span> [Stack2 available_stacks]</pre></td></tr></table></div></div>
<div class="paragraph"><p>The class <code>Stack2</code> in <a href="#xmp-stack2">Listing 14</a> consists of the
earlier definition of the class <code>Stack</code> and is extended by the
class-specific method <code>available_stacks</code>, which returns the
current number of instances of the stack. The final command <code>puts</code>
(line 26) prints 2 to the console.</p></div>
<div class="imageblock" id="img-stack2" style="text-align:center;">
<div class="content">
<img src="stack2.png" alt="stack2.png" />
</div>
<div class="title">Figure 15. Stack2</div>
</div>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p>The class diagram in <a href="#img-stack2">Figure 15</a> shows the
diagrammatic representation of the class object-specific method
<code>available_stacks</code>.  Since every class is a specialization of the
common root class <code>nx::Object</code>, the common root class is often omitted
from the class diagrams, so it was omitted here as well in the diagram.</p></div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_basic_language_features_of_nx">3. Basic Language Features of NX</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_variables_and_properties">3.1. Variables and Properties</h3>
<div class="paragraph"><p>In general, NX does not need variable declarations. It allows to
create or modify variables on the fly by using for example the Tcl
commands <code>set</code> and <code>unset</code>. Depending on the variable name (or more
precisely, depending on the variable name&#8217;s prefix consisting of
colons "<code>:</code>") a variable is either local to a method, or it is an
instance variable, or a global variable.  The rules are:</p></div>
<div class="ulist"><ul>
<li>
<p>
A variable without any colon prefix refers typically to a method
   scoped variable. Such a variable is created during the invocation
   of the method, and it is deleted, when the method ends.  In the
   example below, the variable <code>a</code> is method scoped.
</p>
</li>
<li>
<p>
A variable with a single colon prefix refers to an instance
    variable.  An instance variable is part of the object; when the
    object is destroyed, its instance variables are deleted as well. In the
    example below, the variable <code>b</code> is an instance variable.
</p>
</li>
<li>
<p>
A variable with two leading colons refers to a global variable. The
  lifespan of a globale variable ends when the variable is explicitly
  unset or the script terminates. Variables, which are placed in Tcl
  namespaces, are also global variables. In the example below, the
  variable <code>c</code> is a global variable.
</p>
</li>
</ul></div>
<div class="paragraph" id="xmp-var-resolver"><div class="title">Listing 16: Scopes of Variables</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Foo {

  <span class='nx-keyword'>:method</span> foo args {...}
    <span class='nx-[namespace tail [nx::self]]'># "a" is a method scoped variable
</span>    <span class='nx-keyword'>set</span> a 1
    <span class='nx-[namespace tail [nx::self]]'># "b" is an Instance variable
</span>    <span class='nx-keyword'>set</span> :b 2
    <span class='nx-[namespace tail [nx::self]]'># "c" is a global variable/namespaced variable
</span>    <span class='nx-keyword'>set</span> ::c 3
  }
}</pre></td></tr></table></div></div>
<div class="paragraph"><p><a href="#xmp-var-resolver">Listing 16</a> shows a method <code>foo</code>
of some class <code>Foo</code> referring to differently scoped variables.</p></div>
<div class="sect3">
<h4 id="_properties_configurable_instance_variables">3.1.1. Properties: Configurable Instance Variables</h4>
<div class="paragraph"><p>As described above, there is no need to declare instance variables in
NX. In many cases, a developer might want to define some value
constraints for variables, or to provide defaults, or to make
variables configurable upon object creation. Often, variables are
"inherited", meaning that the variables declared in a general class
are also available in a more specialized class. For these purposes NX
provides <em>variable handlers</em> responsible for the management of
instance variables. We distinguish in NX between configurable
variables (called <code>property</code>) and variables that are not configurable
(called <code>variable</code>).</p></div>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p>A <strong>property</strong> is a definition of a configurable instance variable.</p></div>
</div></div>
<div class="paragraph"><p>The term configurable means that (a) one can provide at creation time of
an instance a value for this variable, and (b), one can query the
value via the accessor function <code>cget</code> and (c), one can change the
value of the variable via <code>configure</code> at runtime. Since the general
accessor function <code>cget</code> and <code>configure</code> are available, an application
developer does not have to program own accessor methods. When value
checkers are provided, each time, the value of the variable is to be
changed, the constrained are checked as well.</p></div>
<div class="imageblock" id="img-person-student" style="text-align:center;">
<div class="content">
<img src="person-student.png" alt="person-student.png" />
</div>
<div class="title">Figure 17. Classes Person and Student</div>
</div>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p>The class diagram above defines the classes <code>Person</code> and
<code>Student</code>. For both classes, configurable instance variable are
specified by defining these as properties. The listing below shows
an implementation of this conceptual model in NX.</p></div>
<div class="paragraph" id="xmp-properties"><div class="title">Listing 18: Properties</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># Define a class Person with properties "name"
</span><span class='nx-[namespace tail [nx::self]]'># and "birthday"
</span><span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Person {
  <span class='nx-keyword'>:property</span> name:required
  <span class='nx-keyword'>:property</span> birthday
}

<span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># Define a class Student as specialization of Person
</span><span class='nx-[namespace tail [nx::self]]'># with additional properties
</span><span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Student -superclass Person {
  <span class='nx-keyword'>:property</span> matnr:required
  <span class='nx-keyword'>:property</span> {oncampus:boolean true}
}

<span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># Create instances using configure parameters
</span><span class='nx-[namespace tail [nx::self]]'># for the initialization
</span><span class='nx-[namespace tail [nx::self]]'>#
</span>Person <span class='nx-keyword'>create</span> p1 -name Bob
Student <span class='nx-keyword'>create</span> s1 -name Susan -matnr 4711

<span class='nx-[namespace tail [nx::self]]'># Access property value via accessor method
</span><span class='nx-keyword'>puts</span> <span class='nx-string'>"The name of s1 is [s1 cget -name]"</span></pre></td></tr></table></div></div>
<div class="paragraph"><p>By defining <code>name</code> and <code>birthday</code> as properties of <code>Person</code>, NX makes
these configurable.  When we create an instance of <code>Person</code> named
<code>p1</code>, we can provide a value for e.g. the name by specifying <code>-name</code>
during creation. The properties result in non-positional configure parameters
which can be provided in any order. In our listing, we create an instance of
<code>Person</code> using the configure  parameter <code>name</code> and provide the value of
<code>Bob</code> to the instance variable <code>name</code>.</p></div>
<div class="paragraph"><p>The class <code>Student</code> is defined as a specialization of <code>Person</code> with
two additional properties: <code>matnr</code> and <code>oncampus</code>. The property
<code>matnr</code> is required (it has to be provided, when an instance of this
class is created), and the property <code>oncampus</code> is boolean, and is per
default set to <code>true</code>. Note that the class <code>Student</code> inherits the
properties of <code>Person</code>. So, <code>Student</code> has four properties in total.</p></div>
<div class="paragraph"><p>The property definitions provide the <code>configure parameters</code> for
instance creation. Many other languages require such parameters to be
passed via arguments of a constructor, which is often error prone,
when values are to be passed to superclasses. Also in dynamic
languages, the relationships between classes can be easily changed,
and different superclasses might have different requirements in their
constructors. The declarative approach in NX reduces the need for
tailored constructor methods significantly.</p></div>
<div class="paragraph"><p>Note, that the property <code>matnr</code> of class <code>Student</code> is required. This
means, that if we try to create an instance of <code>Student</code>, a runtime
exception will be triggered. The property <code>oncamups</code> is boolean and
contains a default value. Providing a default value means that
whenever we create an instance of this class the object will contain
such an instance variable, even when we provide no value via the
configure parameters.</p></div>
<div class="paragraph"><p>In our listing, we create an instance of <code>Student</code> using the two
configure parameters <code>name</code> and <code>matnr</code>. Finally, we use method <code>cget</code>
to obtain the value of the instance variable <code>name</code> of object <code>s1</code>.</p></div>
</div>
<div class="sect3">
<h4 id="_non_configurable_instance_variables">3.1.2. Non-configurable Instance Variables</h4>
<div class="paragraph"><p>In practice, not all instance variables should be configurable. But
still, we want to be able to provide defaults similar to
properties. To define non-configurable instance variables the
predefined method <code>variable</code> can be used. Such instance variables are
often used for e.g. keeping the internal state of an object.  The
usage of <code>variable</code> is in many respects similar to <code>property</code>. One
difference is, that <code>property</code> uses the same syntax as for method
parameters, whereas <code>variable</code> receives the default value as a
separate argument (similar to the <code>variable</code> command in plain
Tcl). The introductory Stack example in <a href="#xmp-class-stack">Listing 2</a> uses already the method <code>variable</code>.</p></div>
<div class="paragraph" id="xmp-variable"><div class="title">Listing 19: Declaring Variables</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Base {
  <span class='nx-keyword'>:variable</span> x 1
  <span class='nx-[namespace tail [nx::self]]'># ...
</span>}

<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Derived -superclass Base {
  <span class='nx-keyword'>:variable</span> y 2
  <span class='nx-[namespace tail [nx::self]]'># ...
</span>}

<span class='nx-[namespace tail [nx::self]]'># Create instance of the class Derived
</span>Derived <span class='nx-keyword'>create</span> d1

<span class='nx-[namespace tail [nx::self]]'># Object d1 has instance variables
</span><span class='nx-[namespace tail [nx::self]]'># x == 1 and y == 2</span></pre></td></tr></table></div></div>
<div class="paragraph"><p>Note that the variable definitions are inherited in the same way as
properties. The example in <a href="#xmp-variable">Listing 19</a> shows a
class <code>Derived</code> that inherits from <code>Base</code>. When an instance <code>d1</code> is
created, it will contain the two instance variables <code>x</code> and <code>y</code>.
Note that the variable declarations from <code>property</code> and <code>variable</code> are
used to initialize (and to configure) the instances variables of an object.</p></div>
<div class="paragraph" id="xmp-constructor"><div class="title">Listing 20: Setting Variables in the Constructor</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Base2 {
 <span class='nx-[namespace tail [nx::self]]'># ...
</span> <span class='nx-keyword'>:method</span> <span class='nx-keyword'>init</span> {} {
   <span class='nx-keyword'>set</span> :x 1
   <span class='nx-[namespace tail [nx::self]]'># ....
</span> }
}

<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Derived2 -superclass Base2 {
 <span class='nx-[namespace tail [nx::self]]'># ...
</span> <span class='nx-keyword'>:method</span> <span class='nx-keyword'>init</span> {} {
   <span class='nx-keyword'>set</span> :y 2
   <span class='nx-keyword'>next</span>
   <span class='nx-[namespace tail [nx::self]]'># ....
</span> }
}

<span class='nx-[namespace tail [nx::self]]'># Create instance of the class Derived2
</span>Derived2 <span class='nx-keyword'>create</span> d2</pre></td></tr></table></div></div>
<div class="paragraph"><p>In many other object oriented languages, the instance variables are
initialized solely by the constructor (similar to class <code>Derived2</code> in
<a href="#xmp-constructor">Listing 20</a>). This approach is certainly
also possible in NX. Note that the approach using constructors
requires an explicit method chaining between the constructors and is
less declarative than the approach in NX using <code>property</code> and <code>variable</code>.</p></div>
<div class="paragraph"><p>Both, <code>property</code> and <code>variable</code> provide much more functionalities. One
can for example declare <code>public</code>, <code>protected</code> or <code>private</code> accessor
methods, or one can define variables to be incremental (for
e.g. adding values to a list of values), or one can define variables
specific behavior.</p></div>
</div>
</div>
<div class="sect2">
<h3 id="_method_definitions">3.2. Method Definitions</h3>
<div class="paragraph"><p>The basic building blocks of an object oriented program are object and
classes, which contain named pieces of code, the methods.</p></div>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p><strong>Methods</strong> are subroutines (pieces of code) associated with objects
and/or classes. A method has a name, receives optionally arguments
during invocation and returns a value.</p></div>
</div></div>
<div class="paragraph"><p>Plain Tcl provides subroutines, which are not associated with objects
or classes. Tcl distinguishes between +proc+s (scripted subroutines)
and  commands (system-languages implemented subroutines).</p></div>
<div class="paragraph"><p>Methods might have different scopes, defining, on which kind of
objects these methods are applicable to. These are described in more
detail later on. For the time being, we deal here with methods defined
on classes, which are applicable for the instance of these classes.</p></div>
<div class="sect3">
<h4 id="_scripted_methods">3.2.1. Scripted Methods</h4>
<div class="paragraph"><p>Since NX is a scripting language, most methods are most likely
scripted methods, in which the method body contains Tcl code.</p></div>
<div class="paragraph" id="xmp-fido1"><div class="title">Listing 21: Scripted method</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-[namespace tail [nx::self]]'># Define a class
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Dog {

  <span class='nx-[namespace tail [nx::self]]'># Define a scripted method for the class
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> bark {} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"[self] Bark, bark, bark."</span>
  }
}

<span class='nx-[namespace tail [nx::self]]'># Create an instance of the class
</span>Dog <span class='nx-keyword'>create</span> fido

<span class='nx-[namespace tail [nx::self]]'># The following line prints "::fido Bark, bark, bark."
</span>fido bark</pre></td></tr></table></div></div>
<div class="paragraph"><p>In the example above we create a class <code>Dog</code> with a scripted method
named <code>bark</code>. The method body defines the code, which is executed when
the method is invoked. In this example, the method <code>bar</code> prints out a
line on the terminal starting with the object name (this is determined
by the built in command <code>self</code>) followed by "Bark, bark, bark.".  This
method is defined on a class and applicable to instances of the class
(here the instance <code>fido</code>).</p></div>
</div>
<div class="sect3">
<h4 id="_c_implemented_methods">3.2.2. C-implemented Methods</h4>
<div class="paragraph"><p>Not all of the methods usable in NX are scripted methods; many
predefined methods are defined in the underlying system language,
which is typically C.  For example, in <a href="#xmp-fido1">Listing 21</a> we
used the method <code>create</code> to create the class <code>Dog</code> and to create the
dog instance <code>fido</code>. These methods are implemented in C in the next
scripting framework.</p></div>
<div class="paragraph"><p>C-implemented methods are not only provided by the underlying
framework but might be as well defined by application developers. This
is an advanced topic, not covered here. However, application developer
might reuse some generic C code to define their own C-implemented
methods. Such methods are for example <em>accessors</em>, <em>forwarders</em> and
<em>aliases</em>.</p></div>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p>An <strong>accessor method</strong> is a method that accesses instance
variables of an object. A call to an accessor
without arguments uses the accessor as a getter, obtaining the actual
value of the associated variable. A call to an accessor with an
argument uses it as a setter, setting the value of the associated
variable.</p></div>
</div></div>
<div class="paragraph"><p>NX provides support for C-implemented accessor methods. Accessors have
already been mentioned in the section about properties. When
the option <code>-accessor public|protected|private</code> is provided to a
<code>variable</code> or <code>property</code> definition, NX creates automatically a
same-named accessors method.</p></div>
<div class="paragraph" id="xmp-fido2"><div class="title">Listing 22: Accessor Methods</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Dog {
 <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> bark {} { <span class='nx-keyword'>puts</span> <span class='nx-string'>"[self] Bark, bark, bark."</span> }
 <span class='nx-keyword'>:method</span> <span class='nx-keyword'>init</span> {} { Tail <span class='nx-keyword'>create</span> [<span class='nx-keyword'>self</span>]::tail}
}

<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Tail {
  <span class='nx-keyword'>:property</span> -accessor <span class='nx-keyword'>public</span> {length:double 5}
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> wag {} {<span class='nx-keyword'>return</span> Joy}
}

<span class='nx-[namespace tail [nx::self]]'># Create an instance of the class
</span>Dog <span class='nx-keyword'>create</span> fido

<span class='nx-[namespace tail [nx::self]]'># Use the accessor "length" as a getter, to obtain the value
</span><span class='nx-[namespace tail [nx::self]]'># of a property. The following call returns the length of the
</span><span class='nx-[namespace tail [nx::self]]'># tail of fido
</span>fido::tail length get

<span class='nx-[namespace tail [nx::self]]'># Use the accessor "length" as a setter, to alter the value
</span><span class='nx-[namespace tail [nx::self]]'># of a property. The following call changes the length of
</span><span class='nx-[namespace tail [nx::self]]'># the tail of fido
</span>fido::tail length <span class='nx-keyword'>set</span> 10

<span class='nx-[namespace tail [nx::self]]'># Proving an invalid values will raise an error
</span>fido::tail length <span class='nx-keyword'>set</span> <span class='nx-string'>"Hello"</span></pre></td></tr></table></div></div>
<div class="paragraph"><p><a href="#xmp-fido2">Listing 22</a> shows an extended example, where every dog
has a tail. The object <code>tail</code> is created as a subobject of the dog in
the constructor <code>init</code>. The subobject can be accessed by providing the
full name of the subobject <code>fido::tail</code>. The method <code>length</code> is an
C-implemented accessor, that enforces the value constraint (here a
floating point number, since length uses the value constraint
<code>double</code>). Line 25 will therefore raise an exception, since the
provided values cannot be converted to a double number.</p></div>
<div class="paragraph" id="xmp-fido3"><div class="title">Listing 23: Forwarder Methods</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Dog {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> bark {} { <span class='nx-keyword'>puts</span> <span class='nx-string'>"[self] Bark, bark, bark."</span> }
  <span class='nx-keyword'>:method</span> <span class='nx-keyword'>init</span> {} {
    Tail <span class='nx-keyword'>create</span> [<span class='nx-keyword'>self</span>]::tail
    <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>forward</span> wag [<span class='nx-keyword'>self</span>]::tail wag
  }
}

<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Tail {
  <span class='nx-keyword'>:property</span> {length 5}
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> wag {} {<span class='nx-keyword'>return</span> Joy}
}

<span class='nx-[namespace tail [nx::self]]'># Create an instance of the class
</span>Dog <span class='nx-keyword'>create</span> fido

<span class='nx-[namespace tail [nx::self]]'># The invocation of "fido wag" is delegated to "fido::tail wag".
</span><span class='nx-[namespace tail [nx::self]]'># Therefore, the following method returns "Joy".
</span>fido wag</pre></td></tr></table></div></div>
<div class="paragraph"><p><a href="#xmp-fido3">Listing 23</a> again extends the example by adding a
forwarder named <code>wag</code> to the object (e.g. <code>fido</code>). The forwarder
redirects all calls of the form <code>fido wag</code> with arbitrary arguments to
the subobject <code>fido::tail</code>.</p></div>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p>A <strong>forwarder method</strong> is a
C-implemented method that redirects an invocation for a certain method
to either a method of another object or to some other method of the
same object. Forwarding an invocation of a method to some other
object is a means of delegation.</p></div>
</div></div>
<div class="paragraph"><p>The functionality of the forwarder can just as well be implemented as
a scripted method, but for the most common cases, the forward
implementation is more efficient, and the <code>forward</code> method expresses
the intention of the developer.</p></div>
<div class="paragraph"><p>The method <code>forwarder</code> has several options to change e.g. the order of
the arguments, or to substitute certain patterns in the argument list
etc. This will be described in later sections.</p></div>
</div>
<div class="sect3">
<h4 id="_method_aliases">3.2.3. Method-Aliases</h4>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p>An <strong>alias method</strong> is a means to register either an existing method,
or a Tcl proc, or a Tcl command as a method with the provided
name on a class or object.</p></div>
</div></div>
<div class="paragraph"><p>In some way, the method alias is a restricted form of a forwarder,
though it does not support delegation to different objects or argument
reordering. The advantage of the method alias compared to a forwarder
is that it has close to zero overhead, especially for aliasing
c-implemented methods.</p></div>
<div class="paragraph" id="xmp-fido4"><div class="title">Listing 24: Method-Alias</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Dog {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> bark {} { <span class='nx-keyword'>puts</span> <span class='nx-string'>"[self] Bark, bark, bark."</span> }

  <span class='nx-[namespace tail [nx::self]]'># Define a public alias for the method "bark"
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>alias</span> warn [<span class='nx-keyword'>:info</span> <span class='nx-keyword'>method</span> handle bark]
  <span class='nx-[namespace tail [nx::self]]'># ...
</span>}

<span class='nx-[namespace tail [nx::self]]'># Create an instance of the class
</span>Dog <span class='nx-keyword'>create</span> fido

<span class='nx-[namespace tail [nx::self]]'># The following line prints "::fido Bark, bark, bark."
</span>fido warn</pre></td></tr></table></div></div>
<div class="paragraph"><p><a href="#xmp-fido4">Listing 24</a> extends the last example by defining an
alias for the method <code>bark</code>. The example only shows the bare
mechanism. In general, method aliases are very powerful means for
reusing pre-existing functionality. The full object system of NX and
XOTcl2 is built from aliases, reusing functionality provided by the
next scripting framework under different names. Method aliases
are as well a means for implementing traits in NX.</p></div>
</div>
</div>
<div class="sect2">
<h3 id="_method_protection">3.3. Method Protection</h3>
<div class="paragraph"><p>All kinds of methods might have different kind of protections in NX.
The call-protection defines from which calling context methods might
be called. The Next Scripting Framework supports as well redefinition
protection for methods.</p></div>
<div class="paragraph"><p>NX distinguishes between <code>public</code>, <code>protected</code> and <code>private</code> methods,
where the default call-protection is <code>protected</code>.</p></div>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p>A <strong>public</strong> method can be called from every context. A <strong>protected</strong>
method can only be invoked from the same object. A <strong>private</strong> method
can only be invoked from methods defined on the same entity
(defined on the same class or on the same object) via the invocation
with the local flag (i.e. "<code>: -local foo</code>").</p></div>
</div></div>
<div class="paragraph"><p>All kind of method protections are applicable for all kind of methods,
either scripted or C-implemented.</p></div>
<div class="paragraph"><p>The distinction between public and protected leads to interfaces for
classes and objects. Public methods are intended for consumers of
these entities. Public methods define the intended ways of providing
methods for external usages (usages, from other objects or
classes). Protected methods are intended for the implementor of the
class or subclasses and not for public usage. The distinction between
protected and public reduces the coupling between consumers and the
implementation, and offers more flexibility to the developer.</p></div>
<div class="paragraph" id="xmp-protected-method"><div class="title">Listing 25: Protected Methods</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Foo {

  <span class='nx-[namespace tail [nx::self]]'># Define a public method
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo {} {
    <span class='nx-[namespace tail [nx::self]]'># ....
</span>    <span class='nx-keyword'>return</span> [:helper]
  }

  <span class='nx-[namespace tail [nx::self]]'># Define a protected method
</span>  <span class='nx-keyword'>:method</span> helper {} {
     <span class='nx-keyword'>return</span> 1
  }
}

<span class='nx-[namespace tail [nx::self]]'># Create an instance of the class:
</span>Foo <span class='nx-keyword'>create</span> f1

<span class='nx-[namespace tail [nx::self]]'># The invocation of the public method "foo" returns 1
</span>f1 foo

<span class='nx-[namespace tail [nx::self]]'># The invocation of the protected method "helper" raises an error:
</span>f1 helper</pre></td></tr></table></div></div>
<div class="paragraph"><p>The example above uses <code>:protected method helper &#8230;</code>. We could have
used here as well <code>:method helper &#8230;</code>, since the default method
call-protection is already protected.</p></div>
<div class="paragraph"><p>The method call-protection of <code>private</code> goes one step further and
helps to hide implementation details also for implementors of
subclasses. Private methods are a means for avoiding unanticipated name
clashes. Consider the following example:</p></div>
<div class="paragraph" id="xmp-private-method"><div class="title">Listing 26: Private Methods</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Base {
  <span class='nx-keyword'>:private</span> <span class='nx-keyword'>method</span> helper {a b} {<span class='nx-keyword'>expr</span> {<span class='nx-[namespace tail [nx::self]]'>$a</span> + <span class='nx-[namespace tail [nx::self]]'>$b</span>}}
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo     {a b} {: -local helper <span class='nx-[namespace tail [nx::self]]'>$a</span> <span class='nx-[namespace tail [nx::self]]'>$b</span>}
}

<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Sub -superclass Base {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> bar     {a b} {: -local helper <span class='nx-[namespace tail [nx::self]]'>$a</span> <span class='nx-[namespace tail [nx::self]]'>$b</span>}
  <span class='nx-keyword'>:private</span> <span class='nx-keyword'>method</span> helper {a b} {<span class='nx-keyword'>expr</span> {<span class='nx-[namespace tail [nx::self]]'>$a</span> * <span class='nx-[namespace tail [nx::self]]'>$b</span>}}
  <span class='nx-keyword'>:create</span> s1
}

s1 foo 3 4     ;<span class='nx-[namespace tail [nx::self]]'># returns 7
</span>s1 bar 3 4     ;<span class='nx-[namespace tail [nx::self]]'># returns 12
</span>s1 helper 3 4  ;<span class='nx-[namespace tail [nx::self]]'># raises error: unable to dispatch method helper</span></pre></td></tr></table></div></div>
<div class="paragraph"><p>The base class implements a public method <code>foo</code> using the helper
method named <code>helper</code>. The derived class implements a as well a public
method <code>bar</code>, which is also using a helper method named <code>helper</code>. When
an instance <code>s1</code> is created from the derived class, the method <code>foo</code>
is invoked which uses in turn the private method of the base
class. Therefore, the invocation <code>s1 foo 3 4</code> returns its sum. If
the <code>local</code> flag had not beed used in helper, <code>s1</code> would
have tried to call the helper of <code>Sub</code>, which would be incorrect. For
all other purposes, the private methods are "invisible" in all
situations, e.g., when mixins are used, or within the <code>next</code>-path, etc.</p></div>
<div class="paragraph"><p>By using the <code>-local</code> flag at the call site it is possible to invoke
only the local definition of the method. If we would call the method
without this flag, the resolution order would be the standard
resolution order, starting with filters, mixins, object methods
and the full intrinsic class hierarchy.</p></div>
<div class="paragraph"><p>NX supports the modifier <code>private</code> for methods and properties. In all
cases <code>private</code> is an instrument to avoid unanticipated interactions
and means actually "accessible for methods defined on the same entity
(object or class)". The main usage for <code>private</code> is to improve
locality of the code e.g. for compositional operations.</p></div>
<div class="paragraph"><p>In order to improve locality for properties, a private property
defines therefore internally a variable with a different name to avoid
unintended interactions. The variable should be accessed via the
private accessor, which can be invoked with the <code>-local</code> flag.  In the
following example class <code>D</code> introduces a private property with the
same name as a property in the superclass.</p></div>
<div class="paragraph" id="xmp-private-properties"><div class="title">Listing 27: Private Properties</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># Define a class C with a property "x" and a public accessor
</span><span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> C {
  <span class='nx-keyword'>:property</span> -accessor <span class='nx-keyword'>public</span> {x c}
}

<span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># Define a subclass D with a private property "x"
</span><span class='nx-[namespace tail [nx::self]]'># and a method bar, which is capable of accessing
</span><span class='nx-[namespace tail [nx::self]]'># the private property.
</span><span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> D -superclass C {
  <span class='nx-keyword'>:property</span> -accessor <span class='nx-keyword'>private</span> {x d}
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> bar {p} {<span class='nx-keyword'>return</span> [: -local <span class='nx-[namespace tail [nx::self]]'>$p</span> get]}
}

<span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># The private and public (or protected) properties
</span><span class='nx-[namespace tail [nx::self]]'># define internally separate variable that do not
</span><span class='nx-[namespace tail [nx::self]]'># conflict.
</span><span class='nx-[namespace tail [nx::self]]'>#
</span>D <span class='nx-keyword'>create</span> d1
<span class='nx-keyword'>puts</span> [d1 x get]   ;<span class='nx-[namespace tail [nx::self]]'># prints "c"
</span><span class='nx-keyword'>puts</span> [d1 bar x]   ;<span class='nx-[namespace tail [nx::self]]'># prints "d"</span></pre></td></tr></table></div></div>
<div class="paragraph"><p>Without the <code>private</code> definition of the property, the definition of
property <code>x</code> in class <code>D</code> would shadow the
definition of the property in the superclass <code>C</code> for its instances
(<code>d1 x</code> or <code>set :x</code> would return <code>d</code> instead of <code>c</code>).</p></div>
</div>
<div class="sect2">
<h3 id="_applicability_of_methods">3.4. Applicability of Methods</h3>
<div class="paragraph"><p>As defined above, a method is a subroutine defined on an object or
class. This object (or class) contains the method. If the object (or
class) is deleted, the contained methods will be deleted as well.</p></div>
<div class="sect3">
<h4 id="_instance_methods">3.4.1. Instance Methods</h4>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p>Typically, methods are defined on a class, and the methods defined on the
class are applicable to the instances (direct or indirect) of this
class. These methods are called <strong>instance methods</strong>.</p></div>
</div></div>
<div class="paragraph"><p>In the following example method, <code>foo</code> is an instance method defined
on class <code>C</code>.</p></div>
<div class="paragraph" id="xmp-instance-applicable"><div class="title">Listing 28: Methods applicable for instances</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> C {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo {} {<span class='nx-keyword'>return</span> 1}
  <span class='nx-keyword'>:create</span> c1
}

<span class='nx-[namespace tail [nx::self]]'># Method "foo" is defined on class "C"
</span><span class='nx-[namespace tail [nx::self]]'># and applicable to the instances of "C"
</span>c1 foo</pre></td></tr></table></div></div>
<div class="paragraph"><p>There are many programming languages that only allow these types of methods.
However, NX also allows methods to be defined on objects.</p></div>
</div>
<div class="sect3">
<h4 id="_object_methods">3.4.2. Object Methods</h4>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p>Methods defined on objects are <strong>object methods</strong>. Object
methods are only applicable on the object, on which they are defined.
Object methods cannot be inherited from other objects.</p></div>
</div></div>
<div class="paragraph"><p>The following example defines an object method <code>bar</code> on the
instance <code>c1</code> of class <code>C</code>, and as well as the object specific method
<code>baz</code> defined on the object <code>o1</code>. An object method is defined
via <code>object method</code>.</p></div>
<div class="paragraph"><p>Note that we can define a object method that shadows (redefines)
for this object methods provided from classes.</p></div>
<div class="paragraph" id="xmp-object-applicable1"><div class="title">Listing 29: Object Method</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> C {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo {} {<span class='nx-keyword'>return</span> 1}
  <span class='nx-keyword'>:create</span> c1 {
     <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> foo {} {<span class='nx-keyword'>return</span> 2}
     <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> bar {} {<span class='nx-keyword'>return</span> 3}
  }
}

<span class='nx-[namespace tail [nx::self]]'># Method "bar" is an object specific method of "c1"
</span>c1 bar

<span class='nx-[namespace tail [nx::self]]'># object-specific method "foo" returns 2
</span>c1 foo

<span class='nx-[namespace tail [nx::self]]'># Method "baz" is an object specific method of "o1"
</span><span class='nx-keyword'>nx::Object</span> <span class='nx-keyword'>create</span> o1 {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> baz {} {<span class='nx-keyword'>return</span> 4}
}
o1 baz</pre></td></tr></table></div></div>
</div>
<div class="sect3">
<h4 id="_class_methods">3.4.3. Class Methods</h4>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p>A <strong>class method</strong> is a method defined on a class, which is only
applicable to the class object itself. The class method is actually
an object method of the class object.</p></div>
</div></div>
<div class="paragraph"><p>In NX, all classes are objects. Classes are in NX special kind of
objects that have e.g. the ability to create instances and to provide
methods for the instances. Classes manage their instances. The general
method set for classes is defined on the meta-classes (more about
this later).</p></div>
<div class="paragraph"><p>The following example defines a public class method <code>bar</code> on class
<code>C</code>. The class method is specified by using the modifier <code>object</code> in
front of <code>method</code> in the method definition command.</p></div>
<div class="paragraph" id="xmp-object-applicable2"><div class="title">Listing 30: Class Methods</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> C {
  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-[namespace tail [nx::self]]'># Define a class method "bar" and an instance
</span>  <span class='nx-[namespace tail [nx::self]]'># method "foo"
</span>  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> bar {} {<span class='nx-keyword'>return</span> 2}
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo {} {<span class='nx-keyword'>return</span> 1}

  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-[namespace tail [nx::self]]'># Create an instance of the current class
</span>  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-keyword'>:create</span> c1
}

<span class='nx-[namespace tail [nx::self]]'># Method "bar" is a class method of class "C"
</span><span class='nx-[namespace tail [nx::self]]'># therefore applicable on the class object "C"
</span>C bar

<span class='nx-[namespace tail [nx::self]]'># Method "foo" is an instance method of "C"
</span><span class='nx-[namespace tail [nx::self]]'># therefore applicable on instance "c1"
</span>c1 foo

<span class='nx-[namespace tail [nx::self]]'># When trying to invoke the class method on the
</span><span class='nx-[namespace tail [nx::self]]'># instance, an error will be raised.
</span>c1 bar</pre></td></tr></table></div></div>
<div class="paragraph"><p>In some other object oriented programming languages, class methods
are called "static methods".</p></div>
</div>
</div>
<div class="sect2">
<h3 id="_ensemble_methods">3.5. Ensemble Methods</h3>
<div class="paragraph"><p>NX provides <em>ensemble methods</em> as a means to structure the method name
space and to group related methods. Ensemble methods are similar in
concept to Tcl&#8217;s ensemble commands.</p></div>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p>An <strong>ensemble method</strong> is a form of a hierarchical method consisting of
a container method and sub-methods. The first argument of the
container method is interpreted as a selector (the sub-method). Every
sub-method can be an container method as well.</p></div>
</div></div>
<div class="paragraph"><p>Ensemble methods provide a means to group related commands together,
and they are extensible in various ways. It is possible to add
sub-methods at any time to existing ensembles. Furthermore, it is
possible to extend ensemble methods via mixin classes.</p></div>
<div class="paragraph"><p>The following example defines an ensemble method for <code>string</code>. An
ensemble method is defined when the provide method name contains a
space.</p></div>
<div class="paragraph" id="xmp-ensemble-methods"><div class="title">Listing 31: Ensemble Method</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> C {

    <span class='nx-[namespace tail [nx::self]]'># Define an ensemble method "string" with sub-methods
</span>    <span class='nx-[namespace tail [nx::self]]'># "length", "tolower" and "info"
</span>
    <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> <span class='nx-string'>"string length"</span>  {x} {....}
    <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> <span class='nx-string'>"string tolower"</span> {x} {...}
    <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> <span class='nx-string'>"string info"</span> {x} {...}
    <span class='nx-[namespace tail [nx::self]]'>#...
</span>    <span class='nx-keyword'>:create</span> c1
}

<span class='nx-[namespace tail [nx::self]]'># Invoke the ensemble method
</span>c1 <span class='nx-keyword'>string</span> length <span class='nx-string'>"hello world"</span></pre></td></tr></table></div></div>
</div>
<div class="sect2">
<h3 id="_method_resolution">3.6. Method Resolution</h3>
<div class="paragraph"><p>When a method is invoked, the applicable method is searched in the
following order:</p></div>
Per-object Mixins -> Per-class Mixins -> Object -> Intrinsic Class Hierarchy
<div class="paragraph"><p>In the case, no mixins are involved, first the object is searched for
an object method with the given name, and then the class hierarchy
of the object. The method can be defined multiple times on the search
path, so some of these method definitions might be <em>shadowed</em> by the
more specific definitions.</p></div>
<div class="paragraph" id="xmp-method-resolution"><div class="title">Listing 32: Method Resolution with Intrinsic Classes</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> C {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo {} {
    <span class='nx-keyword'>return</span> <span class='nx-string'>"C foo: [next]"</span>
  }
}

<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> D -superclass C {

  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo {} {
    <span class='nx-keyword'>return</span> <span class='nx-string'>"D foo: [next]"</span>
  }

   <span class='nx-keyword'>:create</span> d1 {
     <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> foo {} {
       <span class='nx-keyword'>return</span> <span class='nx-string'>"d1 foo: [next]"</span>
     }
   }
}

<span class='nx-[namespace tail [nx::self]]'># Invoke the method foo
</span>d1 foo
<span class='nx-[namespace tail [nx::self]]'># result: "d1 foo: D foo: C foo: "
</span>
<span class='nx-[namespace tail [nx::self]]'># Query the precedence order from NX via introspection
</span>d1 <span class='nx-keyword'>info</span> precedence
<span class='nx-[namespace tail [nx::self]]'># result: "::D ::C ::nx::Object"</span></pre></td></tr></table></div></div>
<div class="paragraph"><p>Consider the example in
<a href="#xmp-method-resolution">Listing 32</a>. When the method
<code>foo</code> is invoked on object <code>d1</code>, the object method has the highest
precedence and is therefore invoked. The object methods shadows
the same-named methods in the class hierarchy, namely the method <code>foo</code>
of class <code>D</code> and the method <code>foo</code> of class <code>C</code>. The shadowed methods
can be still invoked, either via the primitive <code>next</code> or via method
handles (we used already method handles in the section about method
aliases). In the example above, <code>next</code> calls the shadowed method and
add their results to the results of every method. So, the final result
contains parts from <code>d1</code>, <code>D</code> and <code>C</code>. Note, that the topmost <code>next</code>
in method <code>foo</code> of class <code>C</code> shadows no method <code>foo</code> and simply
returns empty (and not an error message).</p></div>
<div class="paragraph"><p>The introspection method <code>info precedence</code> provides information about
the order, in which classes processed during method resolution.</p></div>
<div class="paragraph" id="xmp-method-resolution2"><div class="title">Listing 33: Method Resolution with Mixin Classes</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> M1 {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo {} { <span class='nx-keyword'>return</span> <span class='nx-string'>"M1 foo: [next]"</span>}
}
<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> M2 {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo {} { <span class='nx-keyword'>return</span> <span class='nx-string'>"M2 foo: [next]"</span>}
}

<span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># "d1" is created based on the definitions of the last example
</span><span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># Add the methods from "M1" as per-object mixin to "d1"
</span>d1 <span class='nx-keyword'>object</span> mixins add M1

<span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># Add the methods from "M2" as per-class mixin to class "C"
</span>C mixins add M2

<span class='nx-[namespace tail [nx::self]]'># Invoke the method foo
</span>d1 foo
<span class='nx-[namespace tail [nx::self]]'># result: "M1 foo: M2 foo: d1 foo: D foo: C foo: "
</span>
<span class='nx-[namespace tail [nx::self]]'># Query the precedence order from NX via introspection
</span>d1 <span class='nx-keyword'>info</span> precedence
<span class='nx-[namespace tail [nx::self]]'># result: "::M1 ::M2 ::D ::C ::nx::Object"</span></pre></td></tr></table></div></div>
<div class="paragraph"><p>The example in <a href="#xmp-method-resolution2">Listing 33</a> is
an extension of the previous example. We define here two additional
classes <code>M1</code> and <code>M2</code> which are used as per-object and per-class
mixins.  Both classes define the method <code>foo</code>, these methods shadow
the definitions of the intrinsic class hierarchy. Therefore an
invocation of <code>foo</code> on object <code>d1</code> causes first an invocation of
method in the per-object mixin.</p></div>
<div class="paragraph" id="xmp-method-resolution3"><div class="title">Listing 34: Method Invocation Flags</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># "d1" is created based on the definitions of the last two examples,
</span><span class='nx-[namespace tail [nx::self]]'># the mixins "M1" and "M2" are registered.
</span><span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># Define a public object method "bar", which calls the method
</span><span class='nx-[namespace tail [nx::self]]'># "foo" which various invocation options:
</span><span class='nx-[namespace tail [nx::self]]'>#
</span>d1 <span class='nx-keyword'>public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> bar {} {
   <span class='nx-keyword'>puts</span> [:foo]
   <span class='nx-keyword'>puts</span> [: -local foo]
   <span class='nx-keyword'>puts</span> [: -intrinsic foo]
   <span class='nx-keyword'>puts</span> [: -system foo]
}

<span class='nx-[namespace tail [nx::self]]'># Invoke the method "bar"
</span>d1 bar</pre></td></tr></table></div></div>
<div class="paragraph"><p>In the first line of the body of method <code>bar</code>, the method <code>foo</code> is
called as usual with an implicit receiver, which defaults to the
current object (therefore, the call is equivalent to <code>d1 foo</code>). The
next three calls show how to provide flags that influence the method
resolution. The flags can be provided between the colon and the method
name. These flags are used rather seldomly but can be helpful in some
situations.</p></div>
<div class="paragraph"><p>The invocation flag <code>-local</code> means that the method has to be resolved
from the same place, where the current method is defined. Since the
current method is defined as a object method, <code>foo</code> is resolved as
a object method. The effect is that the mixin definitions are
ignored. The invocation flag <code>-local</code> was already introduced int the
section about method protection, where it was used to call <em>private</em>
methods.</p></div>
<div class="paragraph"><p>The invocation flag <code>-intrinsic</code> means that the method has to be resolved
from the intrinsic definitions, meaning simply without mixins. The
effect is here the same as with the invocation flag <code>-local</code>.</p></div>
<div class="paragraph"><p>The invocation flag <code>-system</code> means that the method has to be resolved
from basic - typically predefined - classes of the object system. This
can be useful, when script overloads system methods, but still want to
call the shadowed methods from the base classes. In our case, we have
no definitions of <code>foo</code> on the base clases, therefore an error message
is returned.</p></div>
<div class="paragraph"><p>The output of <a href="#xmp-method-resolution3">Listing 34</a> is:</p></div>
<div class="listingblock">
<div class="content">
<pre><code>   M1 foo: M2 foo: d1 foo: D foo: C foo:
   d1 foo: D foo: C foo:
   d1 foo: D foo: C foo:
   ::d1: unable to dispatch method 'foo'</code></pre>
</div></div>
</div>
<div class="sect2">
<h3 id="_parameters">3.7. Parameters</h3>
<div class="paragraph"><p>NX provides a generalized mechanism for passing values to either
methods (we refer to these as <em>method parameters</em>) or to objects
(these are called <em>configure parameters</em>). Both kind of parameters
might have different features, such as:</p></div>
<div class="ulist"><ul>
<li>
<p>
Positional and non-positional parameters
</p>
</li>
<li>
<p>
Required and non-required parameters
</p>
</li>
<li>
<p>
Default values for parameters
</p>
</li>
<li>
<p>
Value-checking for parameters
</p>
</li>
<li>
<p>
Multiplicity of parameters
</p>
</li>
</ul></div>
<div class="paragraph"><p>TODO: complete list above and provide a short summary of the section</p></div>
<div class="paragraph"><p>Before we discuss method and configure parameters in more detail, we
describe the parameter features in the subsequent sections based on
method parameters.</p></div>
<div class="sect3">
<h4 id="_positional_and_non_positional_parameters">3.7.1. Positional and Non-Positional Parameters</h4>
<div class="paragraph"><p>If the position of a parameter in the list of formal arguments
(e.g. passed to a function) is significant for its meaning, this is a
<em>positional</em> parameter. If the meaning of the parameter is independent
of its position, this is a <em>non-positional</em> parameter. When we call a
method with positional parameters, the meaning of the parameters (the
association with the argument in the argument list of the method) is
determined by its position. When we call a method with non-positional
parameters, their meaning is determined via a name passed with the
argument during invocation.</p></div>
<div class="paragraph" id="xmp-posnonpos"><div class="title">Listing 35: Positional and Non-Positional Method Parameters</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Object</span> <span class='nx-keyword'>create</span> o1 {

  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-[namespace tail [nx::self]]'># Method foo has positional parameters:
</span>  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> foo {x y} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x y=$y"</span>
  }

  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-[namespace tail [nx::self]]'># Method bar has non-positional parameters:
</span>  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> bar {-x -y} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x y=$y"</span>
  }

  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-[namespace tail [nx::self]]'># Method baz has non-positional and
</span>  <span class='nx-[namespace tail [nx::self]]'># positional parameters:
</span>  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> baz {-x -y a} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x? [info exists x] y? [info exists y] a=$a"</span>
  }
}

<span class='nx-[namespace tail [nx::self]]'># invoke foo (positional parameters)
</span>o1 foo 1 2

<span class='nx-[namespace tail [nx::self]]'># invoke bar (non-positional parameters)
</span>o1 bar -y 3 -x 1
o1 bar -x 1 -y 3

<span class='nx-[namespace tail [nx::self]]'># invoke baz (positional and non-positional parameters)
</span>o1 baz -x 1 100
o1 baz 200
o1 baz -- -y</pre></td></tr></table></div></div>
<div class="paragraph"><p>Consider the example in <a href="#xmp-posnonpos">Listing 35</a>. The method
<code>foo</code> has the argument list <code>x y</code>. This means that the first argument
is passed in an invocation like <code>o1 foo 1 2</code> to <code>x</code> (here, the value
<code>1</code>), and the second argument is passed to <code>y</code> (here the value <code>2</code>).
Method <code>bar</code> has in contrary just with non-positional arguments. Here
we pass the names of the parameter together with the values. In the
invocation <code>o1 bar -y 3 -x 1</code> the names of the parameters are prefixed
with a dash ("-"). No matter whether in which order we write the
non-positional parameters in the invocation (see line 30 and 31 in
<a href="#xmp-posnonpos">Listing 35</a>) in both cases the variables <code>x</code>
and <code>y</code> in the body of the method <code>bar</code> get the same values assigned
(<code>x</code> becomes <code>1</code>, <code>y</code> becomes <code>3</code>).</p></div>
<div class="paragraph"><p>It is certainly possible to combine positional and non-positional
arguments. Method <code>baz</code> provides two non-positional parameter (<code>-y</code>
and <code>-y</code>) and one positional parameter (namely <code>a</code>). The invocation in
line 34 passes the value of <code>1</code> to <code>x</code> and the value of <code>100</code> to <code>a</code>.
There is no value passed to <code>y</code>, therefore value of <code>y</code> will be
undefined in the body of <code>baz</code>, <code>info exists y</code> checks for the
existence of the variable <code>y</code> and returns <code>0</code>.</p></div>
<div class="paragraph"><p>The invocation in line 35 passes only a value to the positional
parameter. A more tricky case is in line 36, where we want to pass
<code>-y</code> as a value to the positional parameter <code>a</code>. The case is more
tricky since syntactically the argument parser might consider <code>-y</code> as
the name of one of the non-positional parameter. Therefore we use <code>--</code>
(double dash) to indicate the end of the block of the non-positional
parameters and therefore the value of <code>-y</code> is passed to <code>a</code>.</p></div>
</div>
<div class="sect3">
<h4 id="_optional_and_required_parameters">3.7.2. Optional and Required Parameters</h4>
<div class="paragraph"><p>Per default positional parameters are required, and non-positional
parameters are optional (they can be left out). By using parameter
options, we can as well define positional parameters, which are
optional, and non-positional parameters, which are required.</p></div>
<div class="paragraph" id="xmp-optional-req"><div class="title">Listing 36: Optional and Required Method Parameters</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Object</span> <span class='nx-keyword'>create</span> o2 {

  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-[namespace tail [nx::self]]'># Method foo has one required and one optional
</span>  <span class='nx-[namespace tail [nx::self]]'># positional parameter:
</span>  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> foo {x:required y:optional} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x y? [info exists y]"</span>
  }

  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-[namespace tail [nx::self]]'># Method bar has one required and one optional
</span>  <span class='nx-[namespace tail [nx::self]]'># non-positional parameter:
</span>  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> bar {-x:required -y:optional} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x y? [info exists y]"</span>
  }
}

<span class='nx-[namespace tail [nx::self]]'># invoke foo (one optional positional parameter is missing)
</span>o2 foo 1</pre></td></tr></table></div></div>
<div class="paragraph"><p>The example in <a href="#xmp-optional-req">Listing 36</a> defined method <code>foo</code>
with one required and one optional positional parameter. For this
purpose we use the parameter options <code>required</code> and <code>optional</code>. The
parameter options are separated from the parameter name by a colon. If
there are multiple parameter options, these are separated by commas
(we show this in later examples).</p></div>
<div class="paragraph"><p>The parameter definition <code>x:required</code> for method <code>foo</code> is equivalent
to <code>x</code> without any parameter options (see e.g. previous example),
since positional parameters are per default required. The invocation
in line 21 of <a href="#xmp-optional-req">Listing 36</a> will lead to an
undefined variable <code>y</code> in method <code>foo</code>, because no value us passed to
the optional parameter. Note that only trailing positional parameters might be
optional. If we would call method <code>foo</code> of <a href="#xmp-posnonpos">Listing 35</a> with only one argument, the system would raise an
exception.</p></div>
<div class="paragraph"><p>Similarly, we define method <code>bar</code> in <a href="#xmp-optional-req">Listing 36</a> with one required and one optional non-positional
parameter. The parameter definition <code>-y:optional</code> is equivalent to
<code>-y</code>, since non-positional parameter are per default optional.
However, the non-positional parameter <code>-x:required</code> is required. If we
invoke <code>bar</code> without it, the system will raise an exception.</p></div>
</div>
<div class="sect3">
<h4 id="_default_values_for_parameters">3.7.3. Default Values for Parameters</h4>
<div class="paragraph"><p>Optional parameters might have a default value, which will be used,
when not value is provided for this parameter.  Default values can be
specified for positional and non-positional parameters.</p></div>
<div class="paragraph" id="xmp-default-value"><div class="title">Listing 37: Method Parameters with Default Values</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Object</span> <span class='nx-keyword'>create</span> o3 {

  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-[namespace tail [nx::self]]'># Positional parameter with default value:
</span>  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> foo {{x 1} {y 2}} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x y=$y"</span>
  }

  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-[namespace tail [nx::self]]'># Non-positional parameter with default value:
</span>  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> bar {{-x 10} {-y 20}} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x y=$y"</span>
  }
}

<span class='nx-[namespace tail [nx::self]]'># use default values
</span>o3 foo
o3 bar</pre></td></tr></table></div></div>
<div class="paragraph"><p>In order to define a default value for a parameter, the parameter
specification must be of the form of a 2 element list, where the
second argument is the default value. See for an example in
<a href="#xmp-default-value">Listing 37</a>.</p></div>
</div>
<div class="sect3">
<h4 id="_value_constraints">3.7.4. Value Constraints</h4>
<div class="paragraph"><p>NX provides value constraints for all kind of parameters. By
specifying value constraints a developer can restrict the permissible
values for a parameter and document the expected values in the source
code. Value checking in NX is conditional, it can be turned on or off
in general or on a per-usage level (more about this later). The same
mechanisms can be used not only for input value checking, but as well
for return value checking (we will address this point as well later).</p></div>
<div class="sect4">
<h5 id="_built_in_value_constraints">Built-in Value Constraints</h5>
<div class="paragraph"><p>NX comes with a set of built-in value constraints, which can be
extended on the scripting level. The built-in checkers are either the
native checkers provided directly by the Next Scripting Framework (the
most efficient checkers) or the value checkers provided by Tcl through
<code>string is &#8230;</code>. The built-in checkers have as well the advantage that
they can be used also at any time during bootstrap of an object
system, at a time, when e.g. no objects or methods are defined. The
same checkers are used as well for all C-implemented primitives of NX
and the Next Scripting Framework.</p></div>
<div class="imageblock" id="img-value-checkers" style="text-align:center;">
<div class="content">
<img src="value-checkers.png" alt="value-checkers.png" />
</div>
<div class="title">Figure 38. General Applicable Value Checkers in NX</div>
</div>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p><a href="#img-value-checkers">Figure 38</a> shows the built-in
general applicable value checkers available in NX, which can be used
for all method and configure parameters. In the next step, we show how to
use these value-checkers for checking permissible values for method
parameters. Then we will show, how to provide more detailed value
constraints.</p></div>
<div class="paragraph" id="xmp-value-check"><div class="title">Listing 39: Method Parameters with Value Constraints</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Object</span> <span class='nx-keyword'>create</span> o4 {

  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-[namespace tail [nx::self]]'># Positional parameter with value constraints:
</span>  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> foo {x:integer o<span class='nx-keyword'>:object</span>,optional} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x o? [info exists o]"</span>
  }

  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-[namespace tail [nx::self]]'># Non-positional parameter with value constraints:
</span>  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> bar {{-x:integer 10} {-verbose:boolean false}} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x verbose=$verbose"</span>
  }
}

<span class='nx-[namespace tail [nx::self]]'># The following invocation raises an exception, since the
</span><span class='nx-[namespace tail [nx::self]]'># value "a" for parameter "x" is not an integer
</span>o4 foo a</pre></td></tr></table></div></div>
<div class="paragraph"><p>Value contraints are specified as parameter options in the parameter
specifications. The parameter specification <code>x:integer</code> defines <code>x</code> as
a required positional parmeter which value is constraint to an
integer. The parameter specification <code>o:object,optional</code> shows how to
combine multiple parameter options. The parameter <code>o</code> is an optional
positional parameter, its value must be an object (see
<a href="#xmp-value-check">Listing 39</a>). Value constraints are
specified exactly the same way for non-positional parameters (see
method <code>bar</code> in <a href="#xmp-value-check">Listing 39</a>).</p></div>
<div class="paragraph" id="xmp-check-parameterized"><div class="title">Listing 40: Parameterized Value Constraints</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># Create classes for Person and Project
</span><span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Person
<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Project

<span class='nx-keyword'>nx::Object</span> <span class='nx-keyword'>create</span> o5 {
  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-[namespace tail [nx::self]]'># Parameterized value constraints
</span>  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> work {
     -person<span class='nx-keyword'>:object</span>,type=Person
     -project<span class='nx-keyword'>:object</span>,type=Project
   } {
    <span class='nx-[namespace tail [nx::self]]'># ...
</span>  }
}

<span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># Create a Person and a Project instance
</span><span class='nx-[namespace tail [nx::self]]'>#
</span>Person <span class='nx-keyword'>create</span> gustaf
Project <span class='nx-keyword'>create</span> nx

<span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># Use method with value constraints
</span><span class='nx-[namespace tail [nx::self]]'>#
</span>o5 work -person gustaf -project nx</pre></td></tr></table></div></div>
<div class="paragraph"><p>The native checkers <code>object</code>, <code>class</code>, <code>metaclass</code> and <code>baseclass</code> can
be further specialized with the parameter option <code>type</code> to restrict
the permissible values to instances of certain classes. We can use for
example the native value constraint <code>object</code> either for testing
whether an argument is some object (without further constraints, as in
<a href="#xmp-default-value">Listing 37</a>, method <code>foo</code>), or we can
constrain the value further to some type (direct or indirect instance
of a class). This is shown by method <code>work</code> in
<a href="#xmp-check-parameterized">Listing 40</a> which requires
the parameter <code>-person</code> to be an instance of class <code>Person</code> and the
parameter <code>-project</code> to be an instance of class <code>Project</code>.</p></div>
</div>
<div class="sect4">
<h5 id="_scripted_value_constraints">Scripted Value Constraints</h5>
<div class="paragraph"><p>The set of predefined value checkers can be extended by application
programs via defining methods following certain conventions. The user
defined value checkers are defined as methods of the class <code>nx::Slot</code>
or of one of its subclasses or instances. We will address such cases
in the next sections. In the following example we define two new
value checkers on class <code>nx::Slot</code>. The first value checker is called
<code>groupsize</code>, the second one is called <code>choice</code>.</p></div>
<div class="paragraph" id="xmp-user-types"><div class="title">Listing 41: Scripted Value Checker for Method Parameters</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># Value checker named "groupsize"
</span><span class='nx-[namespace tail [nx::self]]'>#
</span>::nx::Slot <span class='nx-keyword'>method</span> type=groupsize {name value} {
  <span class='nx-keyword'>if</span> {<span class='nx-[namespace tail [nx::self]]'>$value</span> &lt; 1 || <span class='nx-[namespace tail [nx::self]]'>$value</span> &gt; 6} {
    <span class='nx-keyword'>error</span> <span class='nx-string'>"Value '$value' of parameter $name is not between 1 and 6"</span>
  }
}

<span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># Value checker named "choice" with extra argument
</span><span class='nx-[namespace tail [nx::self]]'>#
</span>::nx::Slot <span class='nx-keyword'>method</span> type=choice {name value arg} {
  <span class='nx-keyword'>if</span> {<span class='nx-[namespace tail [nx::self]]'>$value</span> ni [<span class='nx-keyword'>split</span> <span class='nx-[namespace tail [nx::self]]'>$arg</span> |]} {
    <span class='nx-keyword'>error</span> <span class='nx-string'>"Value '$value' of parameter $name not in permissible values $arg"</span>
  }
}

<span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># Create an application class D
</span><span class='nx-[namespace tail [nx::self]]'># using the new value checkers
</span><span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> D {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo {a:groupsize} {
    <span class='nx-[namespace tail [nx::self]]'># ...
</span>  }
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> bar {a:choice,arg=red|yellow|green b:choice,arg=good|bad} {
    <span class='nx-[namespace tail [nx::self]]'># ...
</span>  }
}

D <span class='nx-keyword'>create</span> d1

<span class='nx-[namespace tail [nx::self]]'># testing "groupsize";
</span><span class='nx-[namespace tail [nx::self]]'># the second call (with value 10) will raise an exception:
</span>d1 foo 2
d1 foo 10

<span class='nx-[namespace tail [nx::self]]'># testing "choice"
</span><span class='nx-[namespace tail [nx::self]]'># the second call (with value pink for parameter a)
</span><span class='nx-[namespace tail [nx::self]]'># will raise an exception:
</span>d1 bar green good
d1 bar pink bad</pre></td></tr></table></div></div>
<div class="paragraph"><p>In order to define a checker <code>groupsize</code> a method of the name
<code>type=groupsize</code> is defined. This method receives two arguments,
<code>name</code> and <code>value</code>. The first argument is the name of the parameter
(mostly used for the error message) and the second parameter is
provided value. The value checker simply tests whether the provided
value is between 1 and 3 and raises an exception if this is not the
case (invocation in line 36 in <a href="#xmp-user-types">Listing 41</a>).</p></div>
<div class="paragraph"><p>The checker <code>groupsize</code> has the permissible values defined in its
method&#8217;s body. It is as well possible to define more generic checkers
that can be parameterized. For this parameterization, one can pass an
argument to the checker method (last argument). The checker <code>choice</code>
can be used for restricting the values to a set of predefined
constants. This set is defined in the parameter specification. The
parameter <code>a</code> of method <code>bar</code> in <a href="#xmp-user-types">Listing 41</a>
is restricted to the values <code>red</code>, <code>yellow</code> or <code>green</code>, and the
parameter <code>b</code> is restricted to <code>good</code> or <code>bad</code>. Note that the syntax
of the permissible values is solely defined by the definition of the
value checker in lines 13 to 17. The invocation in line 39 will be ok,
the invocation in line 40 will raise an exception, since <code>pink</code> is not
allowed.</p></div>
<div class="paragraph"><p>If the same checks are used in many places in the program,
defining names for the value checker will be the better choice since
it improves maintainability. For seldomly used kind of checks, the
parameterized value checkers might be more convenient.</p></div>
</div>
</div>
<div class="sect3">
<h4 id="_multiplicity">3.7.5. Multiplicity</h4>
<div class="sidebarblock">
<div class="content">
<div class="paragraph"><p><strong>Multiplicity</strong> is used to define whether a parameter should receive
single or multiple values.</p></div>
</div></div>
<div class="paragraph"><p>A multiplicity specification has a lower and an upper bound. A lower
bound of <code>0</code> means that the value might be empty. A lower bound of <code>1</code>
means that the parameter needs at least one value. The upper bound
might be <code>1</code> or <code>n</code> (or synonymously <code>*</code>). While the upper bound of
<code>1</code> states that at most one value has to be passed, the upper bound of
<code>n</code> says that multiple values are permitted. Other kinds of
multiplicity are currently not allowed.</p></div>
<div class="paragraph"><p>The multiplicity is written as parameter option in the parameter
specification in the form <em>lower-bound</em>..<em>upper-bound</em>. If no
multiplicity is defined the default multiplicity is <code>1..1</code>, which
means: provide exactly one (atomic) value (this was the case in the
previous examples).</p></div>
<div class="paragraph" id="xmp-multiplicity"><div class="title">Listing 42: Method Parameters with Explicit Multiplicity</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-keyword'>nx::Object</span> <span class='nx-keyword'>create</span> o6 {

  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-[namespace tail [nx::self]]'># Positional parameter with an possibly empty
</span>  <span class='nx-[namespace tail [nx::self]]'># single value
</span>  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> foo {x:integer,0..1} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x"</span>
  }

  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-[namespace tail [nx::self]]'># Positional parameter with an possibly empty
</span>  <span class='nx-[namespace tail [nx::self]]'># list of values value
</span>  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> bar {x:integer,0..n} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x"</span>
  }

  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-[namespace tail [nx::self]]'># Positional parameter with a non-empty
</span>  <span class='nx-[namespace tail [nx::self]]'># list of values
</span>  <span class='nx-[namespace tail [nx::self]]'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> baz {x:integer,1..n} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x"</span>
  }
}</pre></td></tr></table></div></div>
<div class="paragraph"><p><a href="#xmp-multiplicity">Listing 42</a> contains three examples for
positional parameters with different multiplicities. Multiplicity is
often combined with value constraints. A parameter specification of
the form <code>x:integer,0..n</code> means that the parameter <code>x</code> receives a list
of integers, which might be empty. Note that the value constraints are
applied to every single element of the list.</p></div>
<div class="paragraph"><p>The parameter specification <code>x:integer,0..1</code> means that <code>x</code> might be
an integer or it might be empty. This is one style of specifying that
no explicit value is passed for a certain parameter. Another style is
to use required or optional parameters. NX does not enforce any
particular style for handling unspecified values.</p></div>
<div class="paragraph"><p>All the examples in <a href="#xmp-multiplicity">Listing 42</a> are for
single positional parameters. Certainly, multiplicity is fully
orthogonal with the other parameter features and can be used as well
for multiple parameters, non-positional parameter, default values,
etc.</p></div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_advanced_language_features">4. Advanced Language Features</h2>
<div class="sectionbody">
<div class="paragraph"><p>&#8230;</p></div>
<div class="sect2">
<h3 id="_objects_classes_and_meta_classes">4.1. Objects, Classes and Meta-Classes</h3>
<div class="paragraph"><p>&#8230;</p></div>
</div>
<div class="sect2">
<h3 id="_resolution_order_and_next_path">4.2. Resolution Order and Next-Path</h3>
<div class="paragraph"><p>&#8230;</p></div>
</div>
<div class="sect2">
<h3 id="_details_on_method_and_configure_parameters">4.3. Details on Method and Configure Parameters</h3>
<div class="paragraph"><p>The parameter specifications are used in NX for the following
purposes. They are used for</p></div>
<div class="ulist"><ul>
<li>
<p>
the specification of input arguments of methods and commands, for
</p>
</li>
<li>
<p>
the specification of return values of methods and commands, and for
</p>
</li>
<li>
<p>
the specification for the initialization of objects.
</p>
</li>
</ul></div>
<div class="paragraph"><p>We refer to the first two as method parameters and the last one as
configure parameters. The examples in the previous sections all parameter
specification were specifications of method parameters.</p></div>
<div class="sidebarblock">
<div class="content">
<div class="paragraph"><p><strong>Method parameters</strong> specify properties about permissible values passed
to methods.</p></div>
</div></div>
<div class="paragraph"><p>The method parameter specify how methods are invoked, how the
actual arguments are passed to local variables of the invoked method
and what kind of checks should be performed on these.</p></div>
<div class="sidebarblock">
<div class="content">
<div class="paragraph"><p><strong>Configure parameters</strong> are parameters that specify, how objects
can be parameterized upon creation.</p></div>
</div></div>
<div class="paragraph"><p>Syntactically, configure parameters and method parameters are the same,
although there are certain differences (e.g. some parameter options
are only applicable for objects parameters, the list of object
parameters is computed dynamically from the class structures, object
parameters are often used in combination with special setter methods,
etc.).  Consider the following example, where we define the two
application classes <code>Person</code> and <code>Student</code> with a few properties.</p></div>
<div class="paragraph" id="xmp-object-parameters"><div class="title">Listing 43: Configure Parameters</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># Define a class Person with properties "name"
</span><span class='nx-[namespace tail [nx::self]]'># and "birthday"
</span><span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Person {
  <span class='nx-keyword'>:property</span> name:required
  <span class='nx-keyword'>:property</span> birthday
}

<span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># Define a class Student as specialization of Person
</span><span class='nx-[namespace tail [nx::self]]'># with and additional property
</span><span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Student -superclass Person {
  <span class='nx-keyword'>:property</span> matnr:required
  <span class='nx-keyword'>:property</span> {oncampus:boolean true}
}

<span class='nx-[namespace tail [nx::self]]'>#
</span><span class='nx-[namespace tail [nx::self]]'># Create instances using configure parameters
</span><span class='nx-[namespace tail [nx::self]]'># for the initialization
</span><span class='nx-[namespace tail [nx::self]]'>#
</span>Person <span class='nx-keyword'>create</span> p1 -name Bob
Student <span class='nx-keyword'>create</span> s1 -name Susan -matnr 4711

<span class='nx-[namespace tail [nx::self]]'># Access property value via "cget" method
</span><span class='nx-keyword'>puts</span> <span class='nx-string'>"The name of s1 is [s1 cget -name]"</span></pre></td></tr></table></div></div>
<div class="paragraph"><p>The class <code>Person</code> has two properties <code>name</code> and <code>birthday</code>, where the
property <code>name</code> is required, the property <code>birthday</code> is not. The
class <code>Student</code> is a subclass of <code>Person</code> with the additional required
property <code>matnr</code> and an optional property <code>oncampus</code> with the
default value <code>true</code> (see <a href="#xmp-object-parameters">Listing 43</a>). The class diagram below visualizes these
definitions.</p></div>
<div class="imageblock" id="img-configure-parameters" style="text-align:center;">
<div class="content">
<img src="configure-parameter.png" alt="configure-parameter.png" />
</div>
<div class="title">Figure 44. System and Application Classes</div>
</div>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p>In NX, these definitions imply that instances of the class of <code>Person</code>
have the properties <code>name</code> and <code>birthday</code> as <em>non-positional object
parameters</em>.  Furthermore it implies that instances of <code>Student</code> will
have the configure parameters of <code>Person</code> augmented with the object
parameters from <code>Student</code> (namely <code>matnr</code> and <code>oncampus</code>).  Based on
these configure parameters, we can create a <code>Person</code> named <code>Bob</code> and a
<code>Student</code> named <code>Susan</code> with the matriculation number <code>4711</code> (see line
23 and 24 in &lt;&lt;xmp-object-parameters,
instance variables <code>name</code>, <code>matnr</code> and <code>oncampus</code> (the latter is
initialized with the default value).</p></div>
<div class="sect3">
<h4 id="_configure_parameters_available_for_all_nx_objects">4.3.1. Configure Parameters available for all NX Objects</h4>
<div class="paragraph"><p>The configure parameters are not limited to the application defined
properties, also NX provides some predefined definitions. Since
<code>Person</code> is a subclass of <code>nx::Object</code> also the configure parameters of
<code>nx::Object</code> are inherited. In the introductory stack example, we used
<code>-mixins</code> applied to an object to denote per-object mixins (see
<a href="#xmp-using-class-safety">Listing 8</a>). Since <code>mixins</code>
is defined as a parameter on <code>nx::Object</code> it can be used as an object
parameter <code>-mixins</code> for all objects in NX. To put it in other words,
every object can be configured to have per-object mixins. If we would
remove this definition, this feature would be removed as well.</p></div>
<div class="paragraph"><p>As shown in the introductory examples, every object can be configured
via a scripted initialization block (the optional scripted block
specified at object creation as last argument; see
<a href="#xmp-object-stack">Listing 5</a> or
<a href="#xmp-object-integer-stack">Listing 12</a>). The
scripted block and its meaning are as well defined by the means of
configure parameters. However, this configure parameter is positional (last
argument) and optional (it can be omitted). The following listing shows
the configure parameters of <code>Person p1</code> and <code>Student s1</code>.</p></div>
<div class="paragraph" id="xmp-object-parameter-list"><div class="title">Listing 45: Computed Actual Configure Parameter</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
</pre></td><td class='nx-body'><pre class='nx'>Configure parameters <span class='nx-keyword'>for</span> Person p1:
   Command:
      p1 <span class='nx-keyword'>info</span> <span class='nx-keyword'>lookup</span> syntax <span class='nx-keyword'>configure</span>
   Result:
      -name <span class='nx-placeholder'>/value/</span> ?-birthday <span class='nx-placeholder'>/value/</span>? ?-object-mixins /mixinreg .../<span class='nx-placeholder'>?
      ?</span>-class /<span class='nx-keyword'>class</span>/? ?-object-filters /filterreg .../? ?/__initblock/?

Configure parameter <span class='nx-keyword'>for</span> Student s1:
   Command:
      s1 <span class='nx-keyword'>info</span> <span class='nx-keyword'>lookup</span> syntax <span class='nx-keyword'>configure</span>
   Result:
      ?-oncampus <span class='nx-placeholder'>/boolean/</span>? -matnr <span class='nx-placeholder'>/value/</span> -name <span class='nx-placeholder'>/value/</span>
      ?-birthday <span class='nx-placeholder'>/value/</span>? ?-object-mixins /mixinreg .../? ?-class /<span class='nx-keyword'>class</span>/<span class='nx-placeholder'>?
      ?</span>-object-filters /filterreg .../? ?/__initblock/?</pre></td></tr></table></div></div>
<div class="paragraph"><p>The given paramter show, how (a) objects can be configured
at runtime or (b) how new instances can be configured
at creation time via the <code>new</code> or <code>create</code> methods.
Introspection can be used to obtain the configuration
parameters from an object via
<code>p1 info lookup parameters configure</code>
(returning the configure parameters currently applicable for
<code>configure</code> or <code>cget</code>) or from a class
<code>Person info lookup parameters create</code> on a class
(returning the configure parameters applicable when an object
of this class is created)</p></div>
<div class="paragraph"><p>The listed configure parameter types <code>mixinreg</code> and
<code>filterreg</code> are for converting definitions of filters and mixins.  The
last value <code>__initblock</code> says that the content of this variable
will be executed in the context of the object being created (before
the constructor <code>init</code> is called).  More about the configure parameter
types later.</p></div>
</div>
<div class="sect3">
<h4 id="_configure_parameters_available_for_all_nx_classes">4.3.2. Configure Parameters available for all NX Classes</h4>
<div class="paragraph"><p>Since classes are certain kind of objects, classes are parameterized
in the same way as objects. A typical parameter for a class definition
is the relation of the class to its superclass.In our example, we have
specified, that <code>Student</code> has <code>Person</code> as superclass via the
non-positional configure parameter <code>-superclass</code>. If no superclass is
specified for a class, the default superclass is
<code>nx::Object</code>. Therefore <code>nx::Object</code> is the default value for the
parameter <code>superclass</code>.</p></div>
<div class="paragraph"><p>Another frequently used parameter for classes is <code>-mixins</code> to denote
per-class mixins (see e.g. the introductory Stack example in
<a href="#xmp-class-safestack">Listing 10</a>), which is defined in
the same way.</p></div>
<div class="paragraph"><p>Since <code>Student</code> is an instance of the meta-class <code>nx::Class</code> it
inherits the configure parameters from <code>nx::Class</code> (see class diagram
<a href="#img-configure-parameters">Figure 44</a>).
Therefore, one can use e.g. <code>-superclass</code> in the definition of classes.</p></div>
<div class="paragraph"><p>Since <code>nx::Class</code> is a subclass of <code>nx::Object</code>, the meta-class
<code>nx::Class</code> inherits the parameter definitions from the most general
class <code>nx::Object</code>. Therefore, every class might as well be configured
with a scripted initialization block the same way as objects can be
configured. We used actually this scripted initialization block in
most examples for defining the methods of the class. The following
listing shows (simplified) the parameters applicable for <code>Class
Student</code>.</p></div>
<div class="paragraph" id="xmp-class-parameter-list"><div class="title">Listing 46: Parameters for Classes</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
</pre></td><td class='nx-body'><pre class='nx'>Configure parameter <span class='nx-keyword'>for</span> <span class='nx-keyword'>class</span> <span class='nx-keyword'>nx::Class</span>
   Command:
      <span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>info</span> <span class='nx-keyword'>lookup</span> syntax <span class='nx-keyword'>configure</span>
   Result:
      ?-superclass /<span class='nx-keyword'>class</span> .../? ?-mixins /mixinreg .../<span class='nx-placeholder'>?
      ?</span>-filters /filterreg .../? ?-object-mixins /mixinreg .../<span class='nx-placeholder'>?
      ?</span>-class /<span class='nx-keyword'>class</span>/? ?-object-filters /filterreg .../? ?/__initblock/?</pre></td></tr></table></div></div>
</div>
<div class="sect3">
<h4 id="_user_defined_parameter_types">4.3.3. User defined Parameter Types</h4>
<div class="paragraph"><p>More detailed definition of the configure parameter types comes here.</p></div>
</div>
<div class="sect3">
<h4 id="_slot_classes_and_slot_objects">4.3.4. Slot Classes and Slot Objects</h4>
<div class="paragraph"><p>In one of the previous sections, we defined scripted (application
defined) checker methods on a class named <code>nx::Slot</code>.  In general NX
offers the possibility to define value checkers not only for all
usages of parameters but as well differently for method parameters or
configure parameters</p></div>
<div class="imageblock" id="img-slots" style="text-align:center;">
<div class="content">
<img src="slots.png" alt="slots.png" />
</div>
<div class="title">Figure 47. Slot Classes and Objects</div>
</div>
<div class="paragraph"><p></p></div>
</div>
<div class="sect3">
<h4 id="_attribute_slots">4.3.5. Attribute Slots</h4>
<div class="paragraph"><p>Still Missing</p></div>
<div class="ulist"><ul>
<li>
<p>
return value checking
</p>
</li>
<li>
<p>
switch
</p>
</li>
<li>
<p>
initcmd &#8230;
</p>
</li>
<li>
<p>
subst rules
</p>
</li>
<li>
<p>
converter
</p>
</li>
<li>
<p>
incremental slots
</p>
</li>
</ul></div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_miscellaneous">5. Miscellaneous</h2>
<div class="sectionbody">
<div class="paragraph"><p>&#8230;</p></div>
<div class="sect2">
<h3 id="_profiling">5.1. Profiling</h3>
<div class="paragraph"><p>&#8230;</p></div>
</div>
<div class="sect2">
<h3 id="_unknown_handlers">5.2. Unknown Handlers</h3>
<div class="paragraph"><p>NX provides two kinds of unknown handlers:</p></div>
<div class="ulist"><ul>
<li>
<p>
Unknown handlers for methods
</p>
</li>
<li>
<p>
Unknown handlers for objects and classes
</p>
</li>
</ul></div>
<div class="sect3">
<h4 id="_unknown_handlers_for_methods">5.2.1. Unknown Handlers for Methods</h4>
<div class="paragraph"><p>Object and classes might be equipped
with a method <code>unknown</code> which is called in cases, where an unknown
method is called. The method unknown receives as first argument the
called method followed by the provided arguments</p></div>
<div class="paragraph" id="xmp-unknown-method"><div class="title">Listing 48: Unknown Method Handler</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
</pre></td><td class='nx-body'><pre class='nx'>::nx::Object <span class='nx-keyword'>create</span> o {
  <span class='nx-keyword'>:object</span> <span class='nx-keyword'>method</span> <span class='nx-keyword'>unknown</span> {called_method args} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"Unknown method '$called_method' called"</span>
  }
}

<span class='nx-[namespace tail [nx::self]]'># Invoke an unknown method for object o:
</span>o foo 1 2 3

<span class='nx-[namespace tail [nx::self]]'># Output will be: "Unknown method 'foo' called"</span></pre></td></tr></table></div></div>
<div class="paragraph"><p>Without any provision of an unknown method handler, an error will be
raised, when an unknown method is called.</p></div>
</div>
<div class="sect3">
<h4 id="_unknown_handlers_for_objects_and_classes">5.2.2. Unknown Handlers for Objects and Classes</h4>
<div class="paragraph"><p>The next scripting framework provides in addition to unknown method
handlers also a means to dynamically create objects and classes, when
these are referenced. This happens e.g. when superclasses, mixins, or
parent objects are referenced. This mechanism can be used to implement
e.g. lazy loading of these classes.  Nsf allows to register multiple
unknown handlers, each identified by a key (a unique name, different
from the keys of other unknown handlers).</p></div>
<div class="paragraph" id="xmp-unknown-class"><div class="title">Listing 49: Unknown Class Handler</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
</pre></td><td class='nx-body'><pre class='nx'>::nx::Class <span class='nx-keyword'>public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> __unknown {name} {
  <span class='nx-[namespace tail [nx::self]]'># A very simple unknown handler, showing just how
</span>  <span class='nx-[namespace tail [nx::self]]'># the mechanism works.
</span>  <span class='nx-keyword'>puts</span> <span class='nx-string'>"***** __unknown called with &lt;$name&gt;"</span>
  ::nx::Class <span class='nx-keyword'>create</span> <span class='nx-[namespace tail [nx::self]]'>$name</span>
}

<span class='nx-[namespace tail [nx::self]]'># Register an unknown handler as a method of ::nx::Class
</span>::nsf::object::unknown::add nx {::nx::Class __unknown}

::nx::Object <span class='nx-keyword'>create</span> o {
  <span class='nx-[namespace tail [nx::self]]'># The class M is unknown at this point
</span>
  <span class='nx-keyword'>:object</span> mixins add M
  <span class='nx-[namespace tail [nx::self]]'># The line above has triggered the unknown class handler,
</span>  <span class='nx-[namespace tail [nx::self]]'># class M is now defined
</span>
  <span class='nx-keyword'>puts</span> [<span class='nx-keyword'>:info</span> <span class='nx-keyword'>object</span> mixins]
  <span class='nx-[namespace tail [nx::self]]'># The output will be:
</span>  <span class='nx-[namespace tail [nx::self]]'>#     ***** __unknown called with &lt;::M&gt;
</span>  <span class='nx-[namespace tail [nx::self]]'>#     ::M
</span>}</pre></td></tr></table></div></div>
<div class="paragraph"><p>The Next Scripting Framework allows to add, query, delete and list unknown handlers.</p></div>
<div class="paragraph" id="xmp-unknown-registration"><div class="title">Listing 50: Unknown Handler registration</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<table class='nx'><tr><td class='nx-linenr'><pre>  1
  2
  3
  4
  5
</pre></td><td class='nx-body'><pre class='nx'><span class='nx-[namespace tail [nx::self]]'># Interface for unknown handlers:
</span><span class='nx-[namespace tail [nx::self]]'># nsf::object::unknown::add /key/ /handler/
</span><span class='nx-[namespace tail [nx::self]]'># nsf::object::unknown::get /key/
</span><span class='nx-[namespace tail [nx::self]]'># nsf::object::unknown::delete /key/
</span><span class='nx-[namespace tail [nx::self]]'># nsf::object::unknown::keys</span></pre></td></tr></table></div></div>
<div class="ulist bibliography"><div class="title">References</div><ul>
<li>
<p>
<a id="Zdun"></a> U. Zdun, M. Strembeck, G. Neumann:
  Object-Based and Class-Based Composition of Transitive Mixins,
  Information and Software Technology, 49(8) 2007 .
</p>
</li>
<li>
<p>
<a id="Neumann and Zdun 1999a"></a> G. Neumann and U. Zdun: Filters as a
   language support for design patterns in object-oriented scripting
   languages. In Proceedings of COOTS&#8217;99, 5th Conference on
   Object-Oriented Technologies and Systems, San Diego, May 1999.
</p>
</li>
<li>
<p>
<a id="Neumann and Zdun 1999b"></a> G. Neumann and U. Zdun: Implementing
   object-specific design patterns using per-object mixins. In Proc. of
   NOSA`99, Second Nordic Workshop on Software Architecture, Ronneby,
   Sweden, August 1999.
</p>
</li>
<li>
<p>
<a id="Neumann and Zdun 1999c"></a> G. Neumann and U. Zdun: Enhancing
   object-based system composition through per-object mixins. In
   Proceedings of Asia-Pacific Software Engineering Conference (APSEC),
   Takamatsu, Japan, December 1999.
</p>
</li>
<li>
<p>
<a id="Neumann and Zdun 2000a"></a> G. Neumann and U. Zdun: XOTCL, an
   object-oriented scripting language. In Proceedings of Tcl2k: The
   7th USENIX Tcl/Tk Conference, Austin, Texas, February 2000.
</p>
</li>
<li>
<p>
<a id="Neumann and Zdun 2000b"></a> G. Neumann and U. Zdun: Towards the Usage
   of Dynamic Object Aggregations as a Form of Composition In:
   Proceedings of Symposium of Applied Computing (SAC&#8217;00), Como,
   Italy, Mar 19-21, 2000.
</p>
</li>
<li>
<p>
<a id="Neumann and Sobernig 2009"></a> G. Neumann, S. Sobernig: XOTcl 2.0 - A
   Ten-Year Retrospective and Outlook, in: Proceedings of the Sixteenth
   Annual Tcl/Tk Conference, Portland, Oregon, October, 2009.
</p>
</li>
<li>
<p>
<a id="Ousterhout 1990"></a> J. K. Ousterhout: Tcl: An embeddable command
   language. In Proc. of the 1990 Winter USENIX Conference, January 1990.
</p>
</li>
<li>
<p>
<a id="Ousterhout 1998"></a> J. K. Ousterhout: Scripting: Higher Level
   Programming for the 21st Century, IEEE Computer 31(3), March 1998.
</p>
</li>
<li>
<p>
<a id="Wetherall and Lindblad 1995"></a> D. Wetherall and C. J. Lindblad: Extending Tcl for
   Dynamic Object-Oriented Programming. Proc. of the Tcl/Tk Workshop '95,
   July 1995.
</p>
</li>
</ul></div>
</div>
</div>
</div>
</div>
</div>
<div id="footnotes"><hr /></div>
<div id="footer">
<div id="footer-text">
Version 2.1.0<br />
Last updated 2016-12-27 18:25:27 CET
</div>
</div>
</body>
</html>