Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #58: high pressure system failure


comp / comp.lang.tcl / Re: oo::class - my variable vs variable

SubjectAuthor
* oo::class - my variable vs variableMark Summerfield
+- Re: oo::class - my variable vs variablePetro Kazmirchuk
+* Re: oo::class - my variable vs variableSchelte
|`* Re: oo::class - my variable vs variableMark Summerfield
| `- Re: oo::class - my variable vs variableet99
+- Re: oo::class - my variable vs variablegreg
`* Re: oo::class - my variable vs variablegreg
 `- Re: oo::class - my variable vs variableet99

1
Subject: oo::class - my variable vs variable
From: Mark Summerfield
Newsgroups: comp.lang.tcl
Date: Thu, 4 Jul 2024 07:17 UTC
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!border-3.nntp.ord.giganews.com!nntp.giganews.com!local-2.nntp.ord.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 04 Jul 2024 07:17:35 +0000
From: mark@qtrac.eu (Mark Summerfield)
Subject: oo::class - my variable vs variable
Newsgroups: comp.lang.tcl
MIME-Version: 1.0
User-Agent: Pan/0.154 (Izium; 517acf4)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Message-ID: <Uh2cncwZ4ewS0Rv7nZ2dnZfqn_udnZ2d@brightview.co.uk>
Date: Thu, 04 Jul 2024 07:17:35 +0000
Lines: 82
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-SKpFQ7hGFxHTtlUl9yZKpfacFwlhrPcjUXi+/ovDi4AGBUNy97MgkWsF4Vo6HrOm7Em5pExSV0QYS41!uCBpClw5jJigKVgiAT+Dd2SzAJdCW/ylG6ksQDePZ2CEkYYSKDPWIs5aiPTYzIGDzXqeA/FDRlxL!YnULg1gXLuNopBXfsnp5lpZQlw==
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
View all headers

I am trying to learn TclOO. I have created two classes P and Q which
appear to have identical behavior. Class P uses "my variable" and Q uses
"variable". Can someone explain the difference between them. (I am
familiar with Python and Go if that's any help with explaining.)

oo::class create P {
constructor {{x 0} {y 0}} {
my variable x_
my variable y_
set x_ $x
set y_ $y
}

method x {} {
my variable x_
return $x_
}

method set_x {x} {
if {![string is integer -strict $x]} {
throw NOT_AN_INT "x must be an int not \"$x\""
}
my variable x_
set x_ $x
}

method y {} {
my variable y_
return $y_
}
}

oo::class create Q {
constructor {{x 0} {y 0}} {
variable x_
variable y_
set x_ $x
set y_ $y
}

method x {} {
variable x_
return $x_
}

method set_x {x} {
if {![string is integer -strict $x]} {
throw NOT_AN_INT "x must be an int not \"$x\""
}
variable x_
set x_ $x
}

method y {} {
variable y_
return $y_
}
}

puts "P"
set p1 [P new]
puts "p1 x=[$p1 x] y=[$p1 y]"
$p1 set_x 5
puts "p1 x=[$p1 x] y=[$p1 y]"
try {$p1 set_x "invalid"} trap {} err { puts $err }
set p2 [P new 0 -8]
puts "p2 x=[$p2 x] y=[$p2 y]"
$p2 set_x 17
puts "p2 x=[$p2 x] y=[$p2 y]"
puts "p1 x=[$p1 x] y=[$p1 y]"

puts "Q"
set q1 [Q new]
puts "q1 x=[$q1 x] y=[$q1 y]"
$q1 set_x 5
puts "q1 x=[$q1 x] y=[$q1 y]"
try {$q1 set_x "invalid"} trap {} err { puts $err }
set q2 [Q new 0 -8]
puts "q2 x=[$q2 x] y=[$q2 y]"
$q2 set_x 17
puts "q2 x=[$q2 x] y=[$q2 y]"
puts "q1 x=[$q1 x] y=[$q1 y]"

Subject: Re: oo::class - my variable vs variable
From: Petro Kazmirchuk
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Fri, 5 Jul 2024 19:47 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: vivid.tree7955@fastmail.com (Petro Kazmirchuk)
Newsgroups: comp.lang.tcl
Subject: Re: oo::class - my variable vs variable
Date: Fri, 5 Jul 2024 21:47:06 +0200
Organization: A noiseless patient Spider
Lines: 100
Message-ID: <v69ijq$3do7s$1@dont-email.me>
References: <Uh2cncwZ4ewS0Rv7nZ2dnZfqn_udnZ2d@brightview.co.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 05 Jul 2024 21:47:06 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="5fb971283a94ae4bdb45a2009645db88";
logging-data="3596540"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+8c0FCOr+IkX1BwH0ZKZI+MFF035UYJFc="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:yNVbdL/8ubLQoCft2ir78mPBmF4=
Content-Language: en-US
In-Reply-To: <Uh2cncwZ4ewS0Rv7nZ2dnZfqn_udnZ2d@brightview.co.uk>
View all headers

On 04/07/2024 09:17, Mark Summerfield wrote:
> I am trying to learn TclOO. I have created two classes P and Q which
> appear to have identical behavior. Class P uses "my variable" and Q uses
> "variable". Can someone explain the difference between them. (I am
> familiar with Python and Go if that's any help with explaining.)
>
> oo::class create P {
> constructor {{x 0} {y 0}} {
> my variable x_
> my variable y_
> set x_ $x
> set y_ $y
> }
>
> method x {} {
> my variable x_
> return $x_
> }
>
> method set_x {x} {
> if {![string is integer -strict $x]} {
> throw NOT_AN_INT "x must be an int not \"$x\""
> }
> my variable x_
> set x_ $x
> }
>
> method y {} {
> my variable y_
> return $y_
> }
> }
>
> oo::class create Q {
> constructor {{x 0} {y 0}} {
> variable x_
> variable y_
> set x_ $x
> set y_ $y
> }
>
> method x {} {
> variable x_
> return $x_
> }
>
> method set_x {x} {
> if {![string is integer -strict $x]} {
> throw NOT_AN_INT "x must be an int not \"$x\""
> }
> variable x_
> set x_ $x
> }
>
> method y {} {
> variable y_
> return $y_
> }
> }
>
> puts "P"
> set p1 [P new]
> puts "p1 x=[$p1 x] y=[$p1 y]"
> $p1 set_x 5
> puts "p1 x=[$p1 x] y=[$p1 y]"
> try {$p1 set_x "invalid"} trap {} err { puts $err }
> set p2 [P new 0 -8]
> puts "p2 x=[$p2 x] y=[$p2 y]"
> $p2 set_x 17
> puts "p2 x=[$p2 x] y=[$p2 y]"
> puts "p1 x=[$p1 x] y=[$p1 y]"
>
> puts "Q"
> set q1 [Q new]
> puts "q1 x=[$q1 x] y=[$q1 y]"
> $q1 set_x 5
> puts "q1 x=[$q1 x] y=[$q1 y]"
> try {$q1 set_x "invalid"} trap {} err { puts $err }
> set q2 [Q new 0 -8]
> puts "q2 x=[$q2 x] y=[$q2 y]"
> $q2 set_x 17
> puts "q2 x=[$q2 x] y=[$q2 y]"
> puts "q1 x=[$q1 x] y=[$q1 y]"

Sorry, can't give an exact answer, but my preference is to put member
variables in the oo::class definition, so that in methods they are
available automatically:

oo::class create MyClass {
# private member variables should be CamelCase
variable Var1 Var2 Var3

constructor {} {
# initialize according to their types:
set Var1 [list]
set Var2 [dict create]
array set Var3 {}
}
}

Subject: Re: oo::class - my variable vs variable
From: Schelte
Newsgroups: comp.lang.tcl
Organization: KPN B.V.
Date: Fri, 5 Jul 2024 20:26 UTC
References: 1
Date: Fri, 5 Jul 2024 22:26:16 +0200
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: oo::class - my variable vs variable
Newsgroups: comp.lang.tcl
References: <Uh2cncwZ4ewS0Rv7nZ2dnZfqn_udnZ2d@brightview.co.uk>
Content-Language: en-US
From: nospam@wanadoo.nl (Schelte)
In-Reply-To: <Uh2cncwZ4ewS0Rv7nZ2dnZfqn_udnZ2d@brightview.co.uk>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <nnd$56db3017$62e898dd@90f8a30a17713fa7>
Organization: KPN B.V.
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!news.swapon.de!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feed.abavia.com!abe007.abavia.com!abp003.abavia.com!news.kpn.nl!not-for-mail
Lines: 29
Injection-Date: Fri, 05 Jul 2024 22:26:16 +0200
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
X-Received-Bytes: 1723
View all headers

On 04/07/2024 09:17, Mark Summerfield wrote:
> I am trying to learn TclOO. I have created two classes P and Q which
> appear to have identical behavior. Class P uses "my variable" and Q uses
> "variable". Can someone explain the difference between them. (I am
> familiar with Python and Go if that's any help with explaining.)
>
When only passing one argument to each of the commands, there is no
functional difference. I normally use "my variable" if I just want to
access one or more instance variables in a method. I use "variable" when
I want to initialize the variables.

So, in the constructor of your example I would use "variable", while I'd
use "my variable" in the methods:

oo::class create P {
constructor {{x 0} {y 0}} {
variable x_ $x y_ $y
}

method x {} {
my variable x_
return $x_
}
}

Schelte.

Subject: Re: oo::class - my variable vs variable
From: greg
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Fri, 5 Jul 2024 21:01 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: gregor.ebbing@gmx.de (greg)
Newsgroups: comp.lang.tcl
Subject: Re: oo::class - my variable vs variable
Date: Fri, 5 Jul 2024 23:01:46 +0200
Organization: A noiseless patient Spider
Lines: 90
Message-ID: <v69mvq$3efr3$1@dont-email.me>
References: <Uh2cncwZ4ewS0Rv7nZ2dnZfqn_udnZ2d@brightview.co.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 05 Jul 2024 23:01:46 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="6f79eaa0d851f6de0cdd04f2db9aef5f";
logging-data="3620707"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+CbQorajXtoKwJMIgjYqwB+8WTz1BGjUg="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:57/wvATE7iw9TWerI/15SN1WI8U=
Content-Language: de-DE
In-Reply-To: <Uh2cncwZ4ewS0Rv7nZ2dnZfqn_udnZ2d@brightview.co.uk>
View all headers

Am 04.07.24 um 09:17 schrieb Mark Summerfield:
> I am trying to learn TclOO. I have created two classes P and Q which
> appear to have identical behavior. Class P uses "my variable" and Q uses
> "variable". Can someone explain the difference between them. (I am
> familiar with Python and Go if that's any help with explaining.)
>
> oo::class create P {
> constructor {{x 0} {y 0}} {
> my variable x_
> my variable y_
> set x_ $x
> set y_ $y
> }
>
> method x {} {
> my variable x_
> return $x_
> }
>
> method set_x {x} {
> if {![string is integer -strict $x]} {
> throw NOT_AN_INT "x must be an int not \"$x\""
> }
> my variable x_
> set x_ $x
> }
>
> method y {} {
> my variable y_
> return $y_
> }
> }
>
> oo::class create Q {
> constructor {{x 0} {y 0}} {
> variable x_
> variable y_
> set x_ $x
> set y_ $y
> }
>
> method x {} {
> variable x_
> return $x_
> }
>
> method set_x {x} {
> if {![string is integer -strict $x]} {
> throw NOT_AN_INT "x must be an int not \"$x\""
> }
> variable x_
> set x_ $x
> }
>
> method y {} {
> variable y_
> return $y_
> }
> }
>
> puts "P"
> set p1 [P new]
> puts "p1 x=[$p1 x] y=[$p1 y]"
> $p1 set_x 5
> puts "p1 x=[$p1 x] y=[$p1 y]"
> try {$p1 set_x "invalid"} trap {} err { puts $err }
> set p2 [P new 0 -8]
> puts "p2 x=[$p2 x] y=[$p2 y]"
> $p2 set_x 17
> puts "p2 x=[$p2 x] y=[$p2 y]"
> puts "p1 x=[$p1 x] y=[$p1 y]"
>
> puts "Q"
> set q1 [Q new]
> puts "q1 x=[$q1 x] y=[$q1 y]"
> $q1 set_x 5
> puts "q1 x=[$q1 x] y=[$q1 y]"
> try {$q1 set_x "invalid"} trap {} err { puts $err }
> set q2 [Q new 0 -8]
> puts "q2 x=[$q2 x] y=[$q2 y]"
> $q2 set_x 17
> puts "q2 x=[$q2 x] y=[$q2 y]"
> puts "q1 x=[$q1 x] y=[$q1 y]"

Hello
if the website is not known:

https://wiki.tcl-lang.org/page/Variables+in+TclOO

Gregor

Subject: Re: oo::class - my variable vs variable
From: Mark Summerfield
Newsgroups: comp.lang.tcl
Date: Sat, 6 Jul 2024 08:54 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!border-1.nntp.ord.giganews.com!nntp.giganews.com!local-4.nntp.ord.giganews.com!Xl.tags.giganews.com!local-2.nntp.ord.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail
NNTP-Posting-Date: Sat, 06 Jul 2024 08:54:38 +0000
From: mark@qtrac.eu (Mark Summerfield)
Subject: Re: oo::class - my variable vs variable
Newsgroups: comp.lang.tcl
References: <Uh2cncwZ4ewS0Rv7nZ2dnZfqn_udnZ2d@brightview.co.uk>
<nnd$56db3017$62e898dd@90f8a30a17713fa7>
MIME-Version: 1.0
User-Agent: Pan/0.149 (Bellevue; 4c157ba)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Message-ID: <tZKcnTx2o7DTmxT7nZ2dnZfqnPGdnZ2d@brightview.co.uk>
Date: Sat, 06 Jul 2024 08:54:38 +0000
Lines: 26
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-H3dmvHZIHxDKnB7PY27Gcze85eNZAtaaVkNGbSyr7IWGw+1I/HEdwl8qQu1kZxE/KWUjpybHDQSNcUE!rfqPAbTTq6GDmFGW60VNW8nLDUjLHTOD9ykf/TGX85Kzt2Gr/gApVNhaXHD+6s4qf+m8YLtnEW88!f6uM0Mc0Q37wdYM+jGDC4h8gnQ==
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
View all headers

On Fri, 5 Jul 2024 22:26:16 +0200, Schelte wrote:

[snip]
> So, in the constructor of your example I would use "variable", while I'd
> use "my variable" in the methods:
>
> oo::class create P {
> constructor {{x 0} {y 0}} {
> variable x_ $x y_ $y
> }
>
> method x {} {
> my variable x_
> return $x_
> }
> }

What I still don't understand is that in method x, _both_ "my variable"
_and_ plain "variable" seem to work.

In fact the only time I've needed "my" so far is in a method call. (But
I'll read the web page that was recommended and am still working my way
through reading Tip 257.)

Subject: Re: oo::class - my variable vs variable
From: et99
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Sun, 7 Jul 2024 01:44 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: et99@rocketship1.me (et99)
Newsgroups: comp.lang.tcl
Subject: Re: oo::class - my variable vs variable
Date: Sat, 6 Jul 2024 18:44:04 -0700
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <v6crt4$1nrm$1@dont-email.me>
References: <Uh2cncwZ4ewS0Rv7nZ2dnZfqn_udnZ2d@brightview.co.uk>
<nnd$56db3017$62e898dd@90f8a30a17713fa7>
<tZKcnTx2o7DTmxT7nZ2dnZfqnPGdnZ2d@brightview.co.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 07 Jul 2024 03:44:05 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="cf7c34038ad1d6d43af07cd82cee13d9";
logging-data="57206"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18PQiM23/g92og1Vc3vM8QI"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.6.1
Cancel-Lock: sha1:us12L5fud/cyKxrYIvnQe1Lqap4=
In-Reply-To: <tZKcnTx2o7DTmxT7nZ2dnZfqnPGdnZ2d@brightview.co.uk>
Content-Language: en-US
View all headers

On 7/6/2024 1:54 AM, Mark Summerfield wrote:
> On Fri, 5 Jul 2024 22:26:16 +0200, Schelte wrote:
>
> [snip]
>> So, in the constructor of your example I would use "variable", while I'd
>> use "my variable" in the methods:
>>
>> oo::class create P {
>> constructor {{x 0} {y 0}} {
>> variable x_ $x y_ $y
>> }
>>
>> method x {} {
>> my variable x_
>> return $x_
>> }
>> }
>
> What I still don't understand is that in method x, _both_ "my variable"
> _and_ plain "variable" seem to work.
>
> In fact the only time I've needed "my" so far is in a method call. (But
> I'll read the web page that was recommended and am still working my way
> through reading Tip 257.)
>
>
>

I don't understand this either. It appears that the my command, which comes from oo::object treats its first argument as a method, and variable like new and create are some inherited methods. The my command seems to in effect replace "my" with the obj you are currently running. The description in oo::object then treats that obj as the "obj" in its documentation.

The effect seems to be that the same sort of thing occurs, a linking of the variables from a namespace into the current context. And I've read that in several places. Syntactically, however, one takes a list of variables, while the other I don't know. The key paragraph on the wiki is:

"They differ importantly in how multiple arguments are treated, so be careful."

Unfortunately, the list of hyperlinks ends there, so I don't know how they differ. At first I guessed it meant that using variable alone, one could also set values, as in the normal variable command inside a namespace. But when I tried that, it made the 2nd argument a variable also.

I'm afraid this overloading of the variable command has me stumped.

Subject: Re: oo::class - my variable vs variable
From: greg
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Sun, 7 Jul 2024 13:21 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: gregor.ebbing@gmx.de (greg)
Newsgroups: comp.lang.tcl
Subject: Re: oo::class - my variable vs variable
Date: Sun, 7 Jul 2024 15:21:39 +0200
Organization: A noiseless patient Spider
Lines: 222
Message-ID: <v6e4p3$bk06$1@dont-email.me>
References: <Uh2cncwZ4ewS0Rv7nZ2dnZfqn_udnZ2d@brightview.co.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 07 Jul 2024 15:21:39 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="dd5c4f34dddbde65219f8c3d73f17eed";
logging-data="380934"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18OejGAPI1t/mS4/zmC+jf3LbZq1M2f6Ys="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:PQrg57y//cWuzpicwi018ZJgmCA=
Content-Language: de-DE
In-Reply-To: <Uh2cncwZ4ewS0Rv7nZ2dnZfqn_udnZ2d@brightview.co.uk>
View all headers

Am 04.07.24 um 09:17 schrieb Mark Summerfield:
> I am trying to learn TclOO. I have created two classes P and Q which
> appear to have identical behavior. Class P uses "my variable" and Q uses
> "variable". Can someone explain the difference between them. (I am
> familiar with Python and Go if that's any help with explaining.)
>

Hello Mark,
I have replaced your demo, hope that's OK.
Different when there are multiple variables as arguments. Originally I
expected the opposite behavior. I had misunderstood that until now.

#DemoClass1 and DemoClass2
# difference constructor with variable individually or together
# difference output for instanceVar2, no initialization with 15 (DemoClass1)
#DemoClass3 and DemoClass4
# no difference with my variable individually or together
# in example:
# objx1 with my variable, x for DemoClass
# objx2 with variable, x for DemoClass

oo::class create DemoClass1 {
constructor {val1 val2} {
variable instanceVar1 instanceVar2
set instanceVar1 $val1
set instanceVar2 $val2
}
method incrementInstanceVarsWithMy {} {
my variable instanceVar1 instanceVar2
incr instanceVar1
incr instanceVar2
}
method incrementInstanceVarsWithVariable {} {
variable instanceVar1
variable instanceVar2
incr instanceVar1
incr instanceVar2
}
method getInstanceVarsWithMy {} {
my variable instanceVar1 instanceVar2
return "instanceVar1: $instanceVar1, \
instanceVar2: $instanceVar2"
}
method getInstanceVarsWithVariable {} {
variable instanceVar1
variable instanceVar2
return "instanceVar1: $instanceVar1, \
instanceVar2: $instanceVar2"
}
} oo::class create DemoClass2 {
constructor {val1 val2} {
variable instanceVar1
variable instanceVar2
set instanceVar1 $val1
set instanceVar2 $val2
}
method incrementInstanceVarsWithMy {} {
my variable instanceVar1 instanceVar2
incr instanceVar1
incr instanceVar2
}
method incrementInstanceVarsWithVariable {} {
variable instanceVar1
variable instanceVar2
incr instanceVar1
incr instanceVar2
}
method getInstanceVarsWithMy {} {
my variable instanceVar1 instanceVar2
return "instanceVar1: $instanceVar1, \
instanceVar2: $instanceVar2"
}
method getInstanceVarsWithVariable {} {
variable instanceVar1
variable instanceVar2
return "instanceVar1: $instanceVar1, \
instanceVar2: $instanceVar2"
}
} oo::class create DemoClass3 {
constructor {val1 val2} {
my variable instanceVar1 instanceVar2
set instanceVar1 $val1
set instanceVar2 $val2
}
method incrementInstanceVarsWithMy {} {
my variable instanceVar1 instanceVar2
incr instanceVar1
incr instanceVar2
}
method incrementInstanceVarsWithVariable {} {
variable instanceVar1
variable instanceVar2
incr instanceVar1
incr instanceVar2
}
method getInstanceVarsWithMy {} {
my variable instanceVar1 instanceVar2
return "instanceVar1: $instanceVar1, \
instanceVar2: $instanceVar2"
}
method getInstanceVarsWithVariable {} {
variable instanceVar1
variable instanceVar2
return "instanceVar1: $instanceVar1, \
instanceVar2: $instanceVar2"
}
}

oo::class create DemoClass4 {
constructor {val1 val2} {
my variable instanceVar1
my variable instanceVar2
set instanceVar1 $val1
set instanceVar2 $val2
}
method incrementInstanceVarsWithMy {} {
my variable instanceVar1
my variable instanceVar2
incr instanceVar1
incr instanceVar2
}
method incrementInstanceVarsWithVariable {} {
variable instanceVar1
variable instanceVar2
incr instanceVar1
incr instanceVar2
}
method getInstanceVarsWithMy {} {
my variable instanceVar1
my variable instanceVar2
return "instanceVar1: $instanceVar1, \
instanceVar2: $instanceVar2"
}
method getInstanceVarsWithVariable {} {
variable instanceVar1
variable instanceVar2
return "instanceVar1: $instanceVar1, \
instanceVar2: $instanceVar2"
}
}

puts "DemoClass1"
puts " obj11"
set obj11 [DemoClass1 new 5 1]
$obj11 incrementInstanceVarsWithMy
puts "obj11: [$obj11 getInstanceVarsWithMy]"
puts " obj12"
set obj12 [DemoClass1 new 15 10]
$obj12 incrementInstanceVarsWithVariable
puts "obj12: [$obj12 getInstanceVarsWithVariable]"

puts \n
puts "DemoClass2"
puts " obj21"
set obj21 [DemoClass2 new 5 1]
$obj21 incrementInstanceVarsWithMy
puts "obj21: [$obj21 getInstanceVarsWithMy]"
puts " obj22"
set obj22 [DemoClass2 new 15 10]
$obj22 incrementInstanceVarsWithVariable
puts "obj22: [$obj22 getInstanceVarsWithVariable]"

puts \n
puts "DemoClass3"
puts " obj31"
set obj31 [DemoClass3 new 5 1]
$obj31 incrementInstanceVarsWithMy
puts "obj31: [$obj31 getInstanceVarsWithMy]"
puts " obj32"
set obj32 [DemoClass3 new 15 10]
$obj32 incrementInstanceVarsWithVariable
puts "obj32: [$obj32 getInstanceVarsWithVariable]"

puts \n
puts "DemoClass4"
puts " obj41"
set obj41 [DemoClass4 new 5 1]
$obj41 incrementInstanceVarsWithMy
puts "obj41: [$obj41 getInstanceVarsWithMy]"
puts " obj42"
set obj42 [DemoClass4 new 15 10]
$obj42 incrementInstanceVarsWithVariable
puts "obj42: [$obj42 getInstanceVarsWithVariable]"

if {0} {
Output:
DemoClass1
obj11
obj11: instanceVar1: 6, instanceVar2: 1
obj12
obj12: instanceVar1: 16, instanceVar2: 1

DemoClass2
obj21
obj21: instanceVar1: 6, instanceVar2: 2
obj22
obj22: instanceVar1: 16, instanceVar2: 11

DemoClass3
obj31
obj31: instanceVar1: 6, instanceVar2: 2
obj32
obj32: instanceVar1: 16, instanceVar2: 11

DemoClass4
obj41
obj41: instanceVar1: 6, instanceVar2: 2
obj42
obj42: instanceVar1: 16, instanceVar2: 11

}

Subject: Re: oo::class - my variable vs variable
From: et99
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Sun, 7 Jul 2024 22:28 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: et99@rocketship1.me (et99)
Newsgroups: comp.lang.tcl
Subject: Re: oo::class - my variable vs variable
Date: Sun, 7 Jul 2024 15:28:06 -0700
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <v6f4po$h1mu$1@dont-email.me>
References: <Uh2cncwZ4ewS0Rv7nZ2dnZfqn_udnZ2d@brightview.co.uk>
<v6e4p3$bk06$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 08 Jul 2024 00:28:09 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="5af1cc5763e1583a036ff83075d43e95";
logging-data="558814"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+sVMDSp61+BZLFv/LDOnDf"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.6.1
Cancel-Lock: sha1:7D4XrcsK3LyJF7N+ml/kVL+4M3g=
In-Reply-To: <v6e4p3$bk06$1@dont-email.me>
Content-Language: en-US
View all headers

On 7/7/2024 6:21 AM, greg wrote:
> Am 04.07.24 um 09:17 schrieb Mark Summerfield:
>> I am trying to learn TclOO. I have created two classes P and Q which
>> appear to have identical behavior. Class P uses "my variable" and Q uses
>> "variable". Can someone explain the difference between them. (I am
>> familiar with Python and Go if that's any help with explaining.)
>>
>
> Hello Mark,
> I have replaced your demo, hope that's OK.
> Different when there are multiple variables as arguments. Originally I expected the opposite behavior. I had misunderstood that until now.
>
>
>
>
> #DemoClass1 and DemoClass2
> # difference constructor with  variable individually or together
> # difference output for instanceVar2, no initialization with 15 (DemoClass1)
> #DemoClass3 and DemoClass4
> # no difference with my variable individually or together
> # in example:
> # objx1 with my variable, x for DemoClass
> # objx2 with variable, x for DemoClass
>
> oo::class create DemoClass1 {
>     constructor {val1 val2} {
>         variable instanceVar1 instanceVar2
>         set instanceVar1 $val1
>         set instanceVar2 $val2
>     }

<snip>

Greg:

Just to (hopefully) clarify with my own findings (Forgive me if this is what you were trying to say in your posting) :

I've stepped this through, and what is happening is that the command in the constructor:

variable instanceVar1 instanceVar2

does indeed do what I thought it was supposed to do (but I must have messed up earlier) and both creates instanceVar1 and initializes it to the string "instanceVar2".

I had wondered why it wouldn't get an error on the increment of a non-integer, until I also saw that it is immediately overwritten by the value of $val1, which is an integer.

instanceVar2 in this constructor is thus merely a local variable that is set to $val2 and then disappears when the constructor returns.

et

1

rocksolid light 0.9.8
clearnet tor