Deutsch   English   Français   Italiano  
<v69mvq$3efr3$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder9.news.weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: greg <gregor.ebbing@gmx.de>
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>
Bytes: 3257

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