1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + | Class create C { + + :method foo args {...} + # Method scoped variable a + set a 1 + # Instance variable b + set :b 2 + # Global variable/namespaced variable c + set ::c 3 + } +} |
Index: doc/next-tutorial.html =================================================================== diff -u -r56136cbbc9bd838be349205d7a7718bbdddb9f9f -r25c826ca05bc7c8e459d273b8990fe9c42505a86 --- doc/next-tutorial.html (.../next-tutorial.html) (revision 56136cbbc9bd838be349205d7a7718bbdddb9f9f) +++ doc/next-tutorial.html (.../next-tutorial.html) (revision 25c826ca05bc7c8e459d273b8990fe9c42505a86) @@ -1068,7 +1068,7 @@
As in the example above, we use :variable to define the - instance variable things for this object (the object stack). + instance variable things for this object (the object stack).
nx::Class create Stack2 { - :public class method available_stacks {} { + :public class method available_stacks {} { return [llength [:info instances]] } @@ -1665,7 +1665,7 @@ 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. -+Listing 17: Optional and Required ParametersListing 17: Optional and Required Method Parameters++
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + Class create C { + + :method foo args {...} + # Method scoped variable a + set a 1 + # Instance variable b + set :b 2 + # Global variable/namespaced variable c + set ::c 3 + } +}+Listing 28 shows a method foo +of some class C referring to differently scoped variables.
+So, in general, there is no need to define or declare instance +variables in NX. However, in some cases, a definition is useful. For +example, one can define properties on classes, which are inherited to +subclasses, and which are used during object initialization. As shown +in the example class Student, properties can have defaults, which +are used as well during initialization.
+In short, a property is an instance variable with accessors, where +the property definition might carry as well value-constraints and a +default value.
+In some cases one would like to define instance variables without +accessors, e.g. for keeping the internal state of an object. For this +purpose, one can use the predefined method variable, which is in +many respects similar to property. One difference is, that +property uses the same syntax as for method parameters, and +variable receives the default value as a separate argument (similar +to the variable command in Tcl. The introductory Stack example in in +Listing 2 used already the method +variable.
+Listing 29: Declaring Variables+++
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + Class create Base { + :variable x 1 + # ... +} + +Class create Derived -superclass Base { + :variable y 2 + # ... +} + +# Create instance of the class Derived +Derived create d1 + +# Object d1 has instance variables +# x == 1 and y == 2+Note that the variable definitions are inherited in the same way as +properties. The example in Listing 29 shows a +class Derived that inherits from Base. When an instance d1 is +created, it will contain the two instance variables x and y.
+Listing 30: Setting Variables in the Constructor+++
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + Class create Base2 { + # ... + :method init {} { + set :x 1 + # .... + } +} + +Class create Derived2 -superclass Base2 { + # ... + :method init {} { + set :y 2 + next + # .... + } +} + +# Create instance of the class Derived2 +Derived2 create d2+ +In many other object oriented languages, the instance variables are +initialized by the constructor, similar to class Derived2 in +Listing 30 . This approach is certainly +as well possible in NX. Note however, that the approach using +constructors requires an explicit method chaining between the +constructors and is less declarative.
+3.2.4. Additional Parameter Types for Object Parameters
More detailed definition of the object parameter types comes here.
-3.2.4. Slot Classes and Slot Objects
+3.2.5. Slot Classes and Slot Objects
In one of the previous sections, we defined scripted (application defined) checker methods on a class named nx::Slot. In general NX offers the possibility to define value checkers not only for all @@ -2450,12 +2640,12 @@
-Figure 28. Slot Classes and Objects+Figure 31. Slot Classes and Objects-3.2.5. Attribute Slots
+3.2.6. Attribute Slots
Still Missing
- @@ -2494,10 +2684,15 @@
-+4. Miscellaneous
+4. Method Protection
+++5. Miscellaneous
+-4.1. Unknown Handlers
+5.1. Unknown Handlers
NX provides two kinds of unknown handlers:
- @@ -2512,12 +2707,12 @@
-4.1.1. Unknown Handlers for Methods
+5.1.1. Unknown Handlers for Methods
-Object and classes might be equipped with a method unknown 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
+Listing 29: Unknown Method HandlerListing 32: Unknown Method Handler