File: rosetta-multiple-inheritance.tcl

package info (click to toggle)
nsf 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 12,628 kB
  • sloc: ansic: 32,245; tcl: 10,636; sh: 664; pascal: 176; lisp: 41; makefile: 24
file content (38 lines) | stat: -rw-r--r-- 1,144 bytes parent folder | download | duplicates (4)
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
#
# == Rosetta example: Inheritance/Multiple
#
# Write two classes (or interfaces) Camera and MobilePhone, then write
# a class CameraPhone which is both a Camera and a MobilePhone.
# 
# https://rosettacode.org/wiki/Inheritance/Multiple
#

package req nx
package req nx::test

#
# NX offers class-based and mixin-based multiple inheritance. The
# search order of features (methods, properties) along the class
# hierarchy is computed using a scheme equivalent with C3
# linearization.
#

#
# a) Class-based multiple inheritance
#
nx::Class create Camera
nx::Class create MobilePhone

nx::Class create CameraPhone -superclasses {Camera MobilePhone}

# Show the resulting class search order:
? {CameraPhone info superclasses -closure} {::Camera ::MobilePhone ::nx::Object}
? {[CameraPhone new] info precedence} {::CameraPhone ::Camera ::MobilePhone ::nx::Object}

# b) Mixin-based multiple inheritance

nx::Class create CameraPhone -mixins {Camera MobilePhone}
? {CameraPhone info mixins} {::Camera ::MobilePhone}

# Show the resulting class search order:
? {[CameraPhone new] info precedence} {::Camera ::MobilePhone ::CameraPhone ::nx::Object}