Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #95: Pentium FDIV bug


comp / comp.lang.tcl / Re: Lost in Ashok's teachings

SubjectAuthor
* Lost in Ashok's teachingsLuc
`* Re: Lost in Ashok's teachingsRalf Fassel
 `* Re: Lost in Ashok's teachingsLuc
  +- Re: Lost in Ashok's teachingsAshok
  +- Re: Lost in Ashok's teachingsRalf Fassel
  `* Re: Lost in Ashok's teachingsRich
   `* Re: Lost in Ashok's teachingsLuc
    `- Re: Lost in Ashok's teachingsRich

1
Subject: Lost in Ashok's teachings
From: Luc
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Thu, 26 Sep 2024 08:04 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: luc@sep.invalid (Luc)
Newsgroups: comp.lang.tcl
Subject: Lost in Ashok's teachings
Date: Thu, 26 Sep 2024 05:04:22 -0300
Organization: A noiseless patient Spider
Lines: 185
Message-ID: <20240926050422.1458aed9@lud1.home>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Injection-Date: Thu, 26 Sep 2024 10:04:23 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="eff6bb106ce815794a174e10665da8da";
logging-data="121662"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/NukmNCkO/HKM+zzSSYbARf1fjQ4SRQCk="
Cancel-Lock: sha1:NoHfqqRxOzGLBZuZRQMKGvyyFDo=
View all headers

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
>>

Subject: Re: Lost in Ashok's teachings
From: Ralf Fassel
Newsgroups: comp.lang.tcl
Date: Thu, 26 Sep 2024 09:21 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: ralfixx@gmx.de (Ralf Fassel)
Newsgroups: comp.lang.tcl
Subject: Re: Lost in Ashok's teachings
Date: Thu, 26 Sep 2024 11:21:14 +0200
Lines: 76
Message-ID: <ygabk0ahit1.fsf@akutech.de>
References: <20240926050422.1458aed9@lud1.home>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net +i7C/jCGDYY0mr9FutlJdgLlPN+P+1e4HnHRof1UTmtpwyPZw=
Cancel-Lock: sha1:0p1P7eARBVeIpg+oqhAaA6ZRY2U= sha1:xw3Le9v1Ry6ILVjfswswVV0wgDc= sha256:aMiJLjv0xswNgwnAxIphxOa70qvhthBNMLCcJnslkQ0=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
View all headers

* Luc <luc@sep.invalid>
| ----------- 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.

You have ommitted the constructor from Ashoks example which initializes
the 'Balance' variable. In your class, this variable is simply not
initialized, which leads to the error.

This is the same with every variable in TCL:

global foo
set foo
=> can't read "foo": no such variable

namespace eval foo {
variable bar
set bar
}
=> can't read "bar": no such variable

You need to initialize (=set) a variable before you can use it.
How the initialization is done depends on the context.

namespace eval foo {
variable bar init-value
set bar
}
=> init-value

Thus if you have variables in a class, you most probably need a
constructor to initialize them.

This is also true for many OO languages...

HTH
R'

Subject: Re: Lost in Ashok's teachings
From: Luc
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Thu, 26 Sep 2024 13:15 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: luc@sep.invalid (Luc)
Newsgroups: comp.lang.tcl
Subject: Re: Lost in Ashok's teachings
Date: Thu, 26 Sep 2024 10:15:00 -0300
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <20240926101500.51d7520f@lud1.home>
References: <20240926050422.1458aed9@lud1.home>
<ygabk0ahit1.fsf@akutech.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 26 Sep 2024 15:15:01 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="eff6bb106ce815794a174e10665da8da";
logging-data="219794"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18zkuYrM8KPPBC7H7mT4Za9igGOteWBInI="
Cancel-Lock: sha1:huGgvsTeq/0ss7UMBEgtxaKjP9Q=
View all headers

On Thu, 26 Sep 2024 11:21:14 +0200, Ralf Fassel wrote:

>You have ommitted the constructor from Ashoks example which initializes
>the 'Balance' variable. In your class, this variable is simply not
>initialized, which leads to the error.

True. Thanks.

What about that plus sign? Where is it defined?

--
Luc
>>

Subject: Re: Lost in Ashok's teachings
From: Ashok
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Thu, 26 Sep 2024 16:47 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: apnmbx-public@yahoo.com (Ashok)
Newsgroups: comp.lang.tcl
Subject: Re: Lost in Ashok's teachings
Date: Thu, 26 Sep 2024 22:17:46 +0530
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <vd437i$8hp5$1@dont-email.me>
References: <20240926050422.1458aed9@lud1.home> <ygabk0ahit1.fsf@akutech.de>
<20240926101500.51d7520f@lud1.home>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 26 Sep 2024 18:47:46 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="0e50792876d7e982a6955fdf8f7b1603";
logging-data="280357"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+1eD2QbS61IrLGiZemovui"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:jEXVXt4i5v1Lb2ac6bUfjnLXgEE=
In-Reply-To: <20240926101500.51d7520f@lud1.home>
Content-Language: en-US
View all headers

On 9/26/2024 6:45 PM, Luc wrote:
> On Thu, 26 Sep 2024 11:21:14 +0200, Ralf Fassel wrote:
>
>> You have ommitted the constructor from Ashoks example which initializes
>> the 'Balance' variable. In your class, this variable is simply not
>> initialized, which leads to the error.
>
> True. Thanks.
>
> What about that plus sign? Where is it defined?
>
>

Replace it with tcl::mathop::+

Somewhere earlier in the book, I think I mention that the rest of the
book assumes a "namespace path tcl::mathop" which is why there is that
standalone "+". Was a bad idea in hindsight just to save the clutter of
tcl::mathop:: prefixes everywhere.

Regarding the code testing, the book build process runs every snippet of
code and the output generated in the book is actually from running each
example at book build time. Very few exceptions to that. Any errors
abort the build process. The OO code should therefore run without errors
(in the book, not sure of the online version which was an earlier draft).

As for the rest of your comments, fair enough that you do not like the
approach. You might try the other books in this space, like the one from
Clif Flynt, or the online tutorial at
https://wiki.tcl-lang.org/page/Tcl+Tutorial+Lesson+OOP1

/Ashok

Subject: Re: Lost in Ashok's teachings
From: Ralf Fassel
Newsgroups: comp.lang.tcl
Date: Thu, 26 Sep 2024 16:53 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: ralfixx@gmx.de (Ralf Fassel)
Newsgroups: comp.lang.tcl
Subject: Re: Lost in Ashok's teachings
Date: Thu, 26 Sep 2024 18:53:11 +0200
Lines: 17
Message-ID: <yga34lmgxvs.fsf@akutech.de>
References: <20240926050422.1458aed9@lud1.home> <ygabk0ahit1.fsf@akutech.de>
<20240926101500.51d7520f@lud1.home>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net 41AUIaXwkx8EwdVgQ0+qhQkIk6mOvgMutgOm02aBR+wBbcNVk=
Cancel-Lock: sha1:XsdJQlm2N1AWjrHqrlzxKPrCfNc= sha1:yDU3D3ENbGZdFJtrLPyvtfpBG1M= sha256:VeV7iUOPFzTOgtiyHfU3fZV0AKY7NsDRfKIAh9clrGM=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
View all headers

* Luc <luc@sep.invalid>
| On Thu, 26 Sep 2024 11:21:14 +0200, Ralf Fassel wrote:
| What about that plus sign? Where is it defined?

That is TCLs mathop:

https://www.tcl-lang.org/man/tcl/TclCmd/mathop.htm

% namespace path {::tcl::mathop}
% + 4 5
9

See also
https://wiki.tcl-lang.org/page/tcl%3A%3Amathop?R=0&O=mathop&W=

HTH
R'

Subject: Re: Lost in Ashok's teachings
From: Rich
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Thu, 26 Sep 2024 22:28 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: rich@example.invalid (Rich)
Newsgroups: comp.lang.tcl
Subject: Re: Lost in Ashok's teachings
Date: Thu, 26 Sep 2024 22:28:17 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <vd4n61$d3p6$1@dont-email.me>
References: <20240926050422.1458aed9@lud1.home> <ygabk0ahit1.fsf@akutech.de> <20240926101500.51d7520f@lud1.home>
Injection-Date: Fri, 27 Sep 2024 00:28:17 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="dcce1bb59e982dc772b78d51c33e5b9d";
logging-data="429862"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+OHjjH79HjCREiLLnacPYl"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.139 (x86_64))
Cancel-Lock: sha1:VlKfSW2TKgIsLWQ8LhKtsciPzRA=
View all headers

Luc <luc@sep.invalid> wrote:
> On Thu, 26 Sep 2024 11:21:14 +0200, Ralf Fassel wrote:
> What about that plus sign? Where is it defined?

A bit of 'context' would help us in actually knowing to what you are
referring. I'm assuming you mean this construct from your original
post:

[+ $var $var]

That + is the "proc" version of the addition operator. It is
accessible as either:

$ rlwrap tclsh
% ::tcl::mathop::+ 1 2
3
%

Or by setting up a namespace path within your current namespace, which
then allows you to call the proc without the absolute namespace names:

% namespace path {::tcl::mathop}
% + 3 4
7

You can also access the math functions (i.e., sin(), abs() etc) in a similar
way (they exist in ::tcl::mathfunc::)

% namespace path {::tcl::mathop ::tcl::mathfunc}
% sin .2
0.19866933079506122
% abs -4
4

Presumably, somewhere in an earlier chapter of Ashok's book, he
detailed the above little setting to be able to call the math operators
(and/or functions) as procs. But as the full book is not on the web,
you would not have had the benefit of having read that material prior
to reading the OO chapter that is available.

Subject: Re: Lost in Ashok's teachings
From: Luc
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Fri, 27 Sep 2024 01:59 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: luc@sep.invalid (Luc)
Newsgroups: comp.lang.tcl
Subject: Re: Lost in Ashok's teachings
Date: Thu, 26 Sep 2024 22:59:56 -0300
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <20240926225956.087d515c@lud1.home>
References: <20240926050422.1458aed9@lud1.home>
<ygabk0ahit1.fsf@akutech.de>
<20240926101500.51d7520f@lud1.home>
<vd4n61$d3p6$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 27 Sep 2024 03:59:57 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="e7b51491f24ca7095b7116c0e7352966";
logging-data="600728"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18fBtxWp6ig8Nx+WviiGRooiE59JRljkjo="
Cancel-Lock: sha1:giRsT71uh66xrWskPd9GJDsWhF4=
View all headers

On Thu, 26 Sep 2024 22:17:46 +0530, Ashok wrote:

>Replace it with tcl::mathop::+
>Somewhere earlier in the book, I think I mention that the rest of the
>book assumes a "namespace path tcl::mathop" which is why there is that
>standalone "+". Was a bad idea in hindsight just to save the clutter of
>tcl::mathop:: prefixes everywhere.

**************************
On Thu, 26 Sep 2024 18:53:11 +0200, Ralf Fassel wrote:

>That is TCLs mathop:
> https://www.tcl-lang.org/man/tcl/TclCmd/mathop.htm
>
> % namespace path {::tcl::mathop}
> % + 4 5
> 9
>
>See also
> https://wiki.tcl-lang.org/page/tcl%3A%3Amathop?R=0&O=mathop&W=

**************************
On Thu, 26 Sep 2024 22:28:17 -0000 (UTC), Rich wrote:

>You can also access the math functions (i.e., sin(), abs() etc) in a
>similar way (they exist in ::tcl::mathfunc::)
>
> % namespace path {::tcl::mathop ::tcl::mathfunc}
> % sin .2
> 0.19866933079506122
> % abs -4
> 4

**************************

I was aware of math operations, but have used them very rarely if ever,
so I didn't make the connection. Most important, I had no idea we could
overload our entire code with all the math operations by importing a
namespace. I'm not sure I like it, but it's good to know.

As usual, I am glad I asked despite my usual hesitation. I always end up
learning when I ask. Many thanks. I really appreciate it.

--
Luc
>>

Subject: Re: Lost in Ashok's teachings
From: Rich
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Fri, 27 Sep 2024 02:48 UTC
References: 1 2 3 4 5
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: rich@example.invalid (Rich)
Newsgroups: comp.lang.tcl
Subject: Re: Lost in Ashok's teachings
Date: Fri, 27 Sep 2024 02:48:38 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <vd56e6$in6f$1@dont-email.me>
References: <20240926050422.1458aed9@lud1.home> <ygabk0ahit1.fsf@akutech.de> <20240926101500.51d7520f@lud1.home> <vd4n61$d3p6$1@dont-email.me> <20240926225956.087d515c@lud1.home>
Injection-Date: Fri, 27 Sep 2024 04:48:39 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="dcce1bb59e982dc772b78d51c33e5b9d";
logging-data="613583"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+NoG8plSHc9ypaBm7KPTfY"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.139 (x86_64))
Cancel-Lock: sha1:7YiSCQzk9BeU/8VFZji2+a55/uU=
View all headers

Luc <luc@sep.invalid> wrote:
>
> I was aware of math operations, but have used them very rarely if ever,
> so I didn't make the connection. Most important, I had no idea we could
> overload our entire code with all the math operations by importing a
> namespace. I'm not sure I like it, but it's good to know.

Small nitpick, "namespace path" is not "importing a namespace".
Importing is an entirely different operation, with its own command to
cause an 'import' (i.e., 'namespace import').

The "namespace path" works in a way analagous to the "PATH="
environment variable for Unix shells. It gives the Tcl interpreter a
list of "places" to look for commands when your code within the
namespace attempts to call the command. I.e., what it does is when the
command you are calling in your current namespace is not defined in
that same namespace, then the interpreter looks in each namespace in
the "path", and if it finds the command defined in one of those
namespaces, that is the command that gets called.

So, if you have this:

namespace eval ::example {
namespace path {::tcl::mathop}
variable a [+ 3 5]
}

Then what happens when the interpreter is executing the variable
command is it see's a call to "+". So first it looks for:

::example::+

But that is not defined (assume the above is the totality of the
definition of ::example).

So next it looks to each namespace in the "namespace path". I.e. it
now looks for:

::tcl::mathop::+

And since that one *is* defined (by Tcl itself) it then executes
::tcl::mathop::+ with parameters 3 and 5.

1

rocksolid light 0.9.8
clearnet tor