Index: doc/next-tutorial.txt =================================================================== diff -u -r457985fc87cb99f06989c8c5b4d415500e7d11d1 -r90ecfc116b3f569cea63dbce061a301497a5746b --- doc/next-tutorial.txt (.../next-tutorial.txt) (revision 457985fc87cb99f06989c8c5b4d415500e7d11d1) +++ doc/next-tutorial.txt (.../next-tutorial.txt) (revision 90ecfc116b3f569cea63dbce061a301497a5746b) @@ -807,7 +807,7 @@ =========================================== *Methods* are subroutines (pieces of code) associated with objects and/or classes. Every method has a name, it receives arguments and -returns a value. +returns a value. =========================================== There are as well other program units, which are not associated with @@ -1107,21 +1107,130 @@ in all situations, e.g. when mixins are used, or within the +next+-path, etc. -By using +my -local+ for the invocaton it is possible to call just the +By using +my -local+ for the invocation it is possible to call just the local definition of the method. If we would call the method as usual, -the resoultion order would be the same as usual, starting with +the resolution order would be the same as usual, starting with filters, mixins, per-object methods and the full intrinsic class hierarchy. -=== Method Scopes +=== Applicability of Methods + +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. + ==== Inherited Methods -==== Class Methods + +=========================================== +Typically, methods are defined on a class and these methods are +applicable to the instances (direct or indirect) of this class. These +methods are *inherited methods*. +=========================================== + +In the following example method +foo+ is an inherited method defined +on class +C+. + +[[xmp-instance-applicable]] +.Listing {counter:figure-number}: Methods applicable for instances +{set:xmp-instance-applicable:Listing {figure-number}} +[source,tcl,numbers] +-------------------------------------------------- +nx::Class create C { + :public method foo {} {return 1} + :create c1 +} + +# Method "foo" is defined on class "C" +# and applicable to the instances of "C" +c1 foo +-------------------------------------------------- + +There are many programming languages that allow only this type of methods. +However, NX allows as well to define methods on objects. + ==== Object Methods +=========================================== +Methods contained in objects are *per-object methods*, which are only +applicable on the object, on which they are defined. +=========================================== + +The following example defines a object specific method +bar+ on the +instance +c1+ of class +C+, and as well the object specific method ++baz+ defined on the object +o1+. Note that we can define a per-object +method that shadows (redefines) for this object an inherited method. + +[[xmp-object-applicable1]] +.Listing {counter:figure-number}: Per-object Method +{set:xmp-object-applicable1:Listing {figure-number}} +[source,tcl,numbers] +-------------------------------------------------- +nx::Class create C { + :public method foo {} {return 1} + :create c1 { + :public method foo {} {return 2} + :public method bar {} {return 3} + } +} + +# Method "bar" is on object specific method of "c1" +c1 bar + +# object-specific method "foo" returns 2 +c1 foo + +# Method "baz" is on object specific method of "o1" +nx::Object create o1 { + :public method baz {} {return 4} +} +o1 baz +-------------------------------------------------- + +==== Class Methods + +========================================= +A *class method* is a method defined on a class, which is only +applicable to the class itself. +========================================= + +In NX, all classes are objects as well. Classes are certainly special +kind of objects, that have the ability to e.g. create instances and to +provide methods to the instances. Classes manage their instances. + +[[xmp-object-applicable2]] +.Listing {counter:figure-number}: Per-object Method +{set:xmp-object-applicable2:Listing {figure-number}} +[source,tcl,numbers] +-------------------------------------------------- +nx::Class create C { + :public class method bar {} {return 2} + :public method foo {} {return 1} + :create c1 +} + +# Method "bar" is a class method of class "C" +C bar + +# Method "foo" is an inherited method of "C" +c1 foo + +# When trying to invoke the class method on the +# instance, an error will be raised. +c1 bar +-------------------------------------------------- + +In several other object oriented programming languages, class methods +are called "static methods". + + === Ensemble Methods ... +=== Method Resolution + +.... + === Parameters NX provides a generalized mechanism for passing values to either