From Newsgroup: comp.lang.tcl
I am trying to learn from this page:
https://www.magicsplat.com/articles/oo.html
It is written by the legendary Ashok. The author that so many people
here so often rush to recommend.
I am not getting along well with it.
His first lesson: create a class:
% oo::class create Account
Next lesson: defining data members:
% oo::define Account {
variable AccountNumber Balance
}
Then, defining methods:
oo::define Account {
method UpdateBalance {change} {
set Balance [+ $Balance $change]
return $Balance
}
method balance {} { return $Balance }
method withdraw {amount} {
return [my UpdateBalance -$amount]
}
method deposit {amount} {
return [my UpdateBalance $amount]
}
export UpdateBalance
}
Sadly, Ashok embraces the most traditional way of teaching programming
or anything related to IT: assuming the reader is some kind of ChatGPT
sponge that can instantly absorb all and any information that is fed
along the way, so he distracts me with a lot of additional information
that I REALLY think should only be fed later, and which I am skipping here.
But I had to take note of this:
"Methods that begin with a lower case letter are exported by default.
Thus in our example, deposit and withdraw are exported methods while UpdateBalance is not. Method visibility can be changed by using the
export and unexport commands inside a oo::define class definition script.
Thus
oo::define Account {export UpdateBalance}
OK. I did that. So here is my script:
----------- oop.tcl -----------------
oo::class create Account
oo::define Account {
variable AccountNumber Balance
}
oo::define Account {
method UpdateBalance {change} {
set Balance [+ $Balance $change]
return $Balance
}
method balance {} { return $Balance }
method withdraw {amount} {
return [my UpdateBalance -$amount]
}
method deposit {amount} {
return [my UpdateBalance $amount]
}
export UpdateBalance
}
----------------------------------
And he provides zero information on how to USE that thing. Zero.
Instead, he goes on and on about a lot of other information that I
REALLY think should be reserved for later.
Finally, I run into "3. Working with objects"
% set acct [Account new 3-14159265]
% Account create smith_account 2-71828182
OK. So I add those two lines to my script. Upon running it, no complaints
by the compiler. But there is no output either. Let's add some action: ----------- oop.tcl -----------------
oo::class create Account
oo::define Account {
variable AccountNumber Balance
}
oo::define Account {
method UpdateBalance {change} {
set Balance [+ $Balance $change]
return $Balance
}
method balance {} { return $Balance }
method withdraw {amount} {
return [my UpdateBalance -$amount]
}
method deposit {amount} {
return [my UpdateBalance $amount]
}
}
oo::define Account {export UpdateBalance}
set acct [Account new 3-14159265]
Account create smith_account 2-71828182
$acct deposit 132
----------------------------------
There, I broke the toy.
can't read "Balance": no such variable
while executing
"+ $Balance $change"
(class "::Account" method "UpdateBalance" line 2)
invoked from within
"my UpdateBalance $amount"
(class "::Account" method "deposit" line 2)
invoked from within
"$acct deposit 132"
(file "oop.tcl" line 41)
Compilation failed.
I tried a lot of ideas and all of them run into the same problem:
the compiler has no knowledge of this Balance variable I speak of.
Reading on, I find this:
3.3. Invoking methods
This is the form used to invoke a method on the object from code
“outside” the object.
% $acct balance
→ 1000000
% $acct deposit 1000
→ 1001000
Oh yeah? OK. Let's add those lines to the script then:
$acct balance
can't read "Balance": no such variable
while executing
"return $Balance "
(class "::Account" method "balance" line 1)
invoked from within
"$acct balance"
(file "oop.tcl" line 41)
Compilation failed.
$acct deposit 1000
can't read "Balance": no such variable
while executing
"+ $Balance $change"
(class "::Account" method "UpdateBalance" line 2)
invoked from within
"my UpdateBalance $amount"
(class "::Account" method "deposit" line 2)
invoked from within
"$acct deposit 1000"
(file "oop.tcl" line 41)
Compilation failed.
I wonder if the code provided in the explanation is really correct
and tested. Why is the 'Balance' variable adamantly not recognized?
I am also intrigued by this line:
set Balance [+ $Balance $change]
Where did he ever define '+' as a command? There is zero explanation
of it.
I am very confused. Any comments, please?
--
Luc
--- Synchronet 3.20a-Linux NewsLink 1.114