Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Emiliano Newsgroups: comp.lang.tcl Subject: Re: Are class consts possible? Date: Sat, 21 Jun 2025 14:03:33 -0300 Organization: A noiseless patient Spider Lines: 49 Message-ID: <20250621140333.d1f4d76c0b58a7a65570a365@example.invalid> References: <10334bd$l3lq$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Injection-Date: Sat, 21 Jun 2025 19:03:38 +0200 (CEST) Injection-Info: dont-email.me; posting-host="d8daf77bd482b02bb6f97b78f3720c8e"; logging-data="1255427"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ZKrKMUtruhYv7miHNxO8zlrbsJtjtqnE=" Cancel-Lock: sha1:nELV4PTy22W2Dwg7PmuOPMlUvaI= X-Newsreader: Sylpheed 3.5.1 (GTK+ 2.24.32; i686-pc-linux-gnu) On Fri, 20 Jun 2025 07:58:37 -0000 (UTC) Mark Summerfield wrote: > Is it possible to create class-specific consts i.e., consts that exist > inside a class's namespace? > > For example, this does _not_ work: > > ``` > > oo::class create Klass { > const SPECIAL special > > method say {} { return $SPECIAL } > } > > set klass [Klass new] > puts "$Klass::SPECIAL [$klass say]" > > ``` Maybe there are more straightforward methods, but this one works: % oo::class create C { method foo {} { # reads the constant classvariable CVar puts $CVar } method bar {} { # tries to set the constant classvariable CVar set CVar "other value" } } ::C % namespace eval [info object namespace C] { # defines the constant in the class namespace const CVar "My class variable" } % C create o ::o % o foo My class variable % o bar can't set "CVar": variable is a constant Regards -- Emiliano