Re: oo::class - my variable vs variable

Liste des GroupesRevenir à cl tcl 
Sujet : Re: oo::class - my variable vs variable
De : vivid.tree7955 (at) *nospam* fastmail.com (Petro Kazmirchuk)
Groupes : comp.lang.tcl
Date : 05. Jul 2024, 20:47:06
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v69ijq$3do7s$1@dont-email.me>
References : 1
User-Agent : Mozilla Thunderbird
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 {}
     }
}

Date Sujet#  Auteur
4 Jul 24 * oo::class - my variable vs variable7Mark Summerfield
5 Jul 24 +- Re: oo::class - my variable vs variable1Petro Kazmirchuk
5 Jul 24 +- Re: oo::class - my variable vs variable1greg
6 Jul 24 +* Re: oo::class - my variable vs variable2Mark Summerfield
7 Jul 24 i`- Re: oo::class - my variable vs variable1et99
7 Jul 24 `* Re: oo::class - my variable vs variable2greg
7 Jul 24  `- Re: oo::class - my variable vs variable1et99

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal