File: rosetta-polymorphism.tcl

package info (click to toggle)
nsf 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 13,208 kB
  • sloc: ansic: 32,687; tcl: 10,723; sh: 660; pascal: 176; javascript: 135; lisp: 41; makefile: 24
file content (47 lines) | stat: -rwxr-xr-x 959 bytes parent folder | download | duplicates (2)
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
44
45
46
47

# == Rosetta Example: Polymorphism
# For details see https://rosettacode.org/wiki/Polymorphism
#
package req nx
package req nx::test

nx::Class create Point {

  :property x:double
  :property y:double

  :public method print {} {
    return "Point(${:x},${:y})"
  }
}

nx::Class create Circle -superclass Point {

  :property radius:double

  :public method print {} {
    return "Circle(${:x},${:y},${:radius})"
  }
}

# === Demonstrating the behavior in a shell: 

# Create a point and get the print string:
? {set p [Point new -x 1.0 -y 2.0]} "::nsf::__#0"
? {$p print} "Point(1.0,2.0)"

# Get the x coordinate of this point:
? {$p cget -x} "1.0"

# Create a circle:
? {set c [Circle new -x 3.0 -y 4.0 -radius 5.0]} "::nsf::__#1"
# Copy the circle
? {set d [$c copy]} "::nsf::__#3"

# Change the radius of the copied circle:
? {$d configure -radius 1.5} ""

# Print the two circles:
? {$c print} "Circle(3.0,4.0,5.0)"

? {$d print} "Circle(3.0,4.0,1.5)"