Deutsch   English   Français   Italiano  
<Uh2cncwZ4ewS0Rv7nZ2dnZfqn_udnZ2d@brightview.co.uk>

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

Path: ...!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 Summerfield <mark@qtrac.eu>
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
Bytes: 2757

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