Prev: keyword highlight in text widget
Next: Postgres
From: Georgios Petasis on 7 Jan 2010 19:28 Hi all, Does anybody knows what is the equivalent of static class methods (in C++) or class procedures (in Itcl) in TclOO? How can I define a "method" inside a class, that can be called without an object? In Itcl somebody can do: itcl::class a { proc static {} { } } a obj obj static a::static How can this be done with TclOO? George
From: Donal K. Fellows on 8 Jan 2010 08:45 On 8 Jan, 00:28, Georgios Petasis <peta...(a)iit.demokritos.gr> wrote: > Does anybody knows what is the equivalent of static class methods (in > C++) or class procedures (in Itcl) in TclOO? I wrote up how to do it in my Tcl2k9 paper, but I've extracted the code and put it on the wiki on the TclOO Tricks page: http://wiki.tcl.tk/21595#pagetoce30e53a1 > How can I define a "method" inside a class, that can be called without > an object? You always call with an object. But classes are objects. :-) The main difference is that instead of being "Foo::bar" you use "Foo bar"; it's two words instead of one. Donal.
From: Georgios Petasis on 8 Jan 2010 11:18 στις 8/1/2010 15:45, O/H Donal K. Fellows έγραψε: > On 8 Jan, 00:28, Georgios Petasis<peta...(a)iit.demokritos.gr> wrote: >> Does anybody knows what is the equivalent of static class methods (in >> C++) or class procedures (in Itcl) in TclOO? > > I wrote up how to do it in my Tcl2k9 paper, but I've extracted the > code and put it on the wiki on the TclOO Tricks page: > http://wiki.tcl.tk/21595#pagetoce30e53a1 > >> How can I define a "method" inside a class, that can be called without >> an object? > > You always call with an object. But classes are objects. :-) The main > difference is that instead of being "Foo::bar" you use "Foo bar"; it's > two words instead of one. > > Donal. Thank you, I will look into this. Since classes are objects, is there a way I can add a method (i.e. cget/configure) for all objects? For example, if I use oo::objdefine oo:object method cget {} {}, will this cget be able to get the value of any variable in any object? (Until now, I add cget/configure in many classes, and I want to correct something. Modifying again all the files is tricky, so I am searching for a way to automate this, and have the implementation of these functions in a single place...) George
From: Georgios Petasis on 8 Jan 2010 13:24 στις 8/1/2010 18:18, O/H Georgios Petasis έγραψε: > στις 8/1/2010 15:45, O/H Donal K. Fellows έγραψε: >> On 8 Jan, 00:28, Georgios Petasis<peta...(a)iit.demokritos.gr> wrote: >>> Does anybody knows what is the equivalent of static class methods (in >>> C++) or class procedures (in Itcl) in TclOO? >> >> I wrote up how to do it in my Tcl2k9 paper, but I've extracted the >> code and put it on the wiki on the TclOO Tricks page: >> http://wiki.tcl.tk/21595#pagetoce30e53a1 >> >>> How can I define a "method" inside a class, that can be called without >>> an object? >> >> You always call with an object. But classes are objects. :-) The main >> difference is that instead of being "Foo::bar" you use "Foo bar"; it's >> two words instead of one. >> >> Donal. > > Thank you, I will look into this. > Since classes are objects, is there a way I can add a method (i.e. > cget/configure) for all objects? > > For example, if I use oo::objdefine oo:object method cget {} {}, will > this cget be able to get the value of any variable in any object? > > (Until now, I add cget/configure in many classes, and I want to correct > something. Modifying again all the files is tricky, so I am searching > for a way to automate this, and have the implementation of these > functions in a single place...) > > George I added the following, and it seems to work: oo::define oo::object method cget {_elep_oo_variable_name} { set _elep_oo_variable_name [string range $_elep_oo_variable_name 1 end] my variable $_elep_oo_variable_name return [set $_elep_oo_variable_name] };# cget oo::define oo::object method configure {args} { foreach {_elep_oo_variable_name _elep_oo_variable_value} $args { set _elep_oo_variable_name [string range $_elep_oo_variable_name 1 end] my variable $_elep_oo_variable_name set $_elep_oo_variable_name $_elep_oo_variable_value } };# configure I also added the static method from the wiki, for defining "common"/"class" variables, and the classmethod, for class methods. The first impression is that they work, but class variables do not seem to be accessible from class methods. Any ideas? George
From: Georgios Petasis on 8 Jan 2010 14:31
στις 8/1/2010 20:24, O/H Georgios Petasis έγραψε: > στις 8/1/2010 18:18, O/H Georgios Petasis έγραψε: >> στις 8/1/2010 15:45, O/H Donal K. Fellows έγραψε: >>> On 8 Jan, 00:28, Georgios Petasis<peta...(a)iit.demokritos.gr> wrote: >>>> Does anybody knows what is the equivalent of static class methods (in >>>> C++) or class procedures (in Itcl) in TclOO? >>> >>> I wrote up how to do it in my Tcl2k9 paper, but I've extracted the >>> code and put it on the wiki on the TclOO Tricks page: >>> http://wiki.tcl.tk/21595#pagetoce30e53a1 >>> >>>> How can I define a "method" inside a class, that can be called without >>>> an object? >>> >>> You always call with an object. But classes are objects. :-) The main >>> difference is that instead of being "Foo::bar" you use "Foo bar"; it's >>> two words instead of one. >>> >>> Donal. >> >> Thank you, I will look into this. >> Since classes are objects, is there a way I can add a method (i.e. >> cget/configure) for all objects? >> >> For example, if I use oo::objdefine oo:object method cget {} {}, will >> this cget be able to get the value of any variable in any object? >> >> (Until now, I add cget/configure in many classes, and I want to correct >> something. Modifying again all the files is tricky, so I am searching >> for a way to automate this, and have the implementation of these >> functions in a single place...) >> >> George > > I added the following, and it seems to work: > > oo::define oo::object method cget {_elep_oo_variable_name} { > set _elep_oo_variable_name [string range $_elep_oo_variable_name 1 end] > my variable $_elep_oo_variable_name > return [set $_elep_oo_variable_name] > };# cget > > oo::define oo::object method configure {args} { > foreach {_elep_oo_variable_name _elep_oo_variable_value} $args { > set _elep_oo_variable_name [string range $_elep_oo_variable_name 1 end] > my variable $_elep_oo_variable_name > set $_elep_oo_variable_name $_elep_oo_variable_value > } > };# configure > > I also added the static method from the wiki, for defining > "common"/"class" variables, and the classmethod, for class methods. > The first impression is that they work, but class variables do not seem > to be accessible from class methods. > Any ideas? > > George The problem seems to be that class variables do not exist, if they have not been initialised (i.e. the constructor has been called once). What I finally did was the following (after Itcl's common): proc ::oo::define::classmethod {name {args ""} {body ""}} { # Create the method on the class if # the caller gave arguments and body # Code from: http://wiki.tcl.tk/21595#pagetoce30e53a1 set argc [llength [info level 0]] if {$argc == 4} { uplevel 1 [list self method $name $args $body] } elseif {$argc == 3} { return -code error "wrong # args: should be \"[lindex [info level 0] 0]\ name ?args body?\"" } # Get the name of the current class set cls [lindex [info level -1] 1] # Get its private "my" command set my [info object namespace $cls]::my # Make the connection by forwarding tailcall forward $name $my $name };# ::oo::define::classmethod proc ::oo::define::common {varname args} { if {[llength $args] > 1} { return -code error "wrong # args: should be \"[lindex [info level 0] 0]\ varName ?init?\"" } # Get the name of the current class set cls [lindex [info level -1] 1] # Export method varname oo::define $cls self export varname # Initialise the variable if {[llength $args]} { set [$cls varname $varname] [lindex $args 0] } };# ::oo::define::common oo::define oo::object method common {args} { if {![llength $args]} return set callclass [lindex [self caller] 0] oo::define $callclass self export varname foreach vname $args { lappend pairs [$callclass varname $vname] $vname } uplevel 1 upvar {*}$pairs };# common Usage: oo::class create test { common var_for_all_objects 0 classmethod test {} { my common var_for_all_objects puts [incr var_for_all_objects] } } [test new] test [test new] test [test new] test George |