27
27
# # Turn off the UI while running a couple of async operations.
28
28
# lock_ui
29
29
#
30
- # set chord [SimpleChord new {
30
+ # set chord [SimpleChord:: new {
31
31
# unlock_ui
32
32
# # Note: $notice here is not referenced in the calling scope
33
33
# if {$notice} { info_popup $notice }
37
37
# # all operations have been initiated.
38
38
# set common_note [$chord add_note]
39
39
#
40
- # # Pass notes as 'after' callbacks to other operations
41
- # async_operation $args [$chord add_note]
42
- # other_async_operation $args [$chord add_note ]
40
+ # # Activate notes in 'after' callbacks to other operations
41
+ # set newnote [$chord add_note]
42
+ # async_operation $args [list $newnote activate ]
43
43
#
44
44
# # Communicate with the chord body
45
45
# if {$condition} {
48
48
# }
49
49
#
50
50
# # Activate the common note, making the chord eligible to complete
51
- # $common_note
51
+ # $common_note activate
52
52
#
53
53
# At this point, the chord will complete at some unknown point in the future.
54
54
# The common note might have been the first note activated, or the async
60
60
# Represents a procedure that conceptually has multiple entrypoints that must
61
61
# all be called before the procedure executes. Each entrypoint is called a
62
62
# "note". The chord is only "completed" when all the notes are "activated".
63
- oo::class create SimpleChord {
64
- variable notes body is_completed
63
+ class SimpleChord {
64
+ field notes
65
+ field body
66
+ field is_completed
67
+ field eval_ns
65
68
66
69
# Constructor:
67
- # set chord [SimpleChord new {body}]
70
+ # set chord [SimpleChord:: new {body}]
68
71
# Creates a new chord object with the specified body script. The
69
72
# body script is evaluated at most once, when a note is activated
70
73
# and the chord has no other non-activated notes.
71
- constructor {body } {
74
+ constructor new {i_body } {
72
75
set notes [list ]
73
- my eval [ list set body $body ]
76
+ set body $i_body
74
77
set is_completed 0
78
+ set eval_ns " [ namespace qualifiers $this ] ::eval"
79
+ return $this
75
80
}
76
81
77
82
# Method:
@@ -80,7 +85,7 @@ oo::class create SimpleChord {
80
85
# the chord body will be evaluated. This can be used to set variable
81
86
# values for the chord body to use.
82
87
method eval {script} {
83
- namespace eval [self] $script
88
+ namespace eval $eval_ns $script
84
89
}
85
90
86
91
# Method:
@@ -92,7 +97,7 @@ oo::class create SimpleChord {
92
97
method add_note {} {
93
98
if {$is_completed } { error " Cannot add a note to a completed chord" }
94
99
95
- set note [ChordNote new [self] ]
100
+ set note [ChordNote:: new $this ]
96
101
97
102
lappend notes $note
98
103
@@ -108,8 +113,8 @@ oo::class create SimpleChord {
108
113
109
114
set is_completed 1
110
115
111
- namespace eval [self] $body
112
- namespace delete [self]
116
+ namespace eval $eval_ns $body
117
+ delete_this
113
118
}
114
119
}
115
120
}
@@ -119,15 +124,17 @@ oo::class create SimpleChord {
119
124
# final note of the chord is activated (this can be any note in the chord,
120
125
# with all other notes already previously activated in any order), the chord's
121
126
# body is evaluated.
122
- oo::class create ChordNote {
123
- variable chord is_activated
127
+ class ChordNote {
128
+ field chord
129
+ field is_activated
124
130
125
131
# Constructor:
126
132
# Instances of ChordNote are created internally by calling add_note on
127
133
# SimpleChord objects.
128
- constructor {chord } {
129
- my eval set chord $chord
134
+ constructor new {c } {
135
+ set chord $c
130
136
set is_activated 0
137
+ return $this
131
138
}
132
139
133
140
# Method:
@@ -138,20 +145,11 @@ oo::class create ChordNote {
138
145
}
139
146
140
147
# Method:
141
- # $note
148
+ # $note activate
142
149
# Activates the note, if it has not already been activated, and
143
150
# completes the chord if there are no other notes awaiting
144
151
# activation. Subsequent calls will have no further effect.
145
- #
146
- # NB: In TclOO, if an object is invoked like a method without supplying
147
- # any method name, then this internal method `unknown` is what
148
- # actually runs (with no parameters). It is used in the ChordNote
149
- # class for the purpose of allowing the note object to be called as
150
- # a function (see example above). (The `unknown` method can also be
151
- # used to support dynamic dispatch, but must take parameters to
152
- # identify the "unknown" method to be invoked. In this form, this
153
- # proc serves only to make instances behave directly like methods.)
154
- method unknown {} {
152
+ method activate {} {
155
153
if {!$is_activated } {
156
154
set is_activated 1
157
155
$chord notify_note_activation
0 commit comments