• Are class consts possible?

    From Mark Summerfield@m.n.summerfield@gmail.com to comp.lang.tcl on Fri Jun 20 07:58:37 2025
    From Newsgroup: comp.lang.tcl

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

    ```
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Rich@rich@example.invalid to comp.lang.tcl on Fri Jun 20 13:28:13 2025
    From Newsgroup: comp.lang.tcl

    Mark Summerfield <m.n.summerfield@gmail.com> wrote:
    Is it possible to create class-specific consts i.e., consts that exist inside a class's namespace?


    Tcl does not have "constants" in the vien of other languages
    "constants".

    However, there are several ways to "roll your own": https://wiki.tcl-lang.org/page/constants

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Mark Summerfield@m.n.summerfield@gmail.com to comp.lang.tcl on Fri Jun 20 19:45:03 2025
    From Newsgroup: comp.lang.tcl

    On Fri, 20 Jun 2025 13:28:13 -0000 (UTC), Rich wrote:

    Mark Summerfield <m.n.summerfield@gmail.com> wrote:
    Is it possible to create class-specific consts i.e., consts that exist
    inside a class's namespace?


    Tcl does not have "constants" in the vien of other languages
    "constants".

    However, there are several ways to "roll your own": https://wiki.tcl-lang.org/page/constants

    According to https://www.tcl-lang.org/man/tcl9.0/TclCmd/const.html
    "const — create and initialize a constant"

    And I use them quite a lot since I'm using Tcl 9.

    Perhaps the wiki needs updating?
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Rich@rich@example.invalid to comp.lang.tcl on Fri Jun 20 20:28:25 2025
    From Newsgroup: comp.lang.tcl

    Mark Summerfield <m.n.summerfield@gmail.com> wrote:
    On Fri, 20 Jun 2025 13:28:13 -0000 (UTC), Rich wrote:

    Mark Summerfield <m.n.summerfield@gmail.com> wrote:
    Is it possible to create class-specific consts i.e., consts that exist
    inside a class's namespace?


    Tcl does not have "constants" in the vien of other languages
    "constants".

    However, there are several ways to "roll your own":
    https://wiki.tcl-lang.org/page/constants

    According to https://www.tcl-lang.org/man/tcl9.0/TclCmd/const.html
    "const — create and initialize a constant"

    And I use them quite a lot since I'm using Tcl 9.

    Ah, it seems Tcl 9 added "const" capabilities.

    Perhaps the wiki needs updating?

    Indeed, that would seem to be the case.

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Mark Summerfield@m.n.summerfield@gmail.com to comp.lang.tcl on Sat Jun 21 06:51:11 2025
    From Newsgroup: comp.lang.tcl

    On Fri, 20 Jun 2025 20:28:25 -0000 (UTC), Rich wrote:

    Mark Summerfield <m.n.summerfield@gmail.com> wrote:
    On Fri, 20 Jun 2025 13:28:13 -0000 (UTC), Rich wrote:

    Mark Summerfield <m.n.summerfield@gmail.com> wrote:
    Is it possible to create class-specific consts i.e., consts that
    exist inside a class's namespace?


    Tcl does not have "constants" in the vien of other languages
    "constants".

    However, there are several ways to "roll your own":
    https://wiki.tcl-lang.org/page/constants

    According to https://www.tcl-lang.org/man/tcl9.0/TclCmd/const.html
    "const — create and initialize a constant"

    And I use them quite a lot since I'm using Tcl 9.

    Ah, it seems Tcl 9 added "const" capabilities.

    Perhaps the wiki needs updating?

    Indeed, that would seem to be the case.

    I've updated the web page https://wiki.tcl-lang.org/page/constants accordingly.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Emiliano@emiliano@example.invalid to comp.lang.tcl on Sat Jun 21 14:03:33 2025
    From Newsgroup: comp.lang.tcl

    On Fri, 20 Jun 2025 07:58:37 -0000 (UTC)
    Mark Summerfield <m.n.summerfield@gmail.com> 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
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Emiliano@emiliano@example.invalid to comp.lang.tcl on Sat Jun 21 18:16:15 2025
    From Newsgroup: comp.lang.tcl

    On Sat, 21 Jun 2025 14:03:33 -0300
    Emiliano <emiliano@example.invalid> wrote:

    On Fri, 20 Jun 2025 07:58:37 -0000 (UTC)
    Mark Summerfield <m.n.summerfield@gmail.com> 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:

    Answering myself:

    % oo::class create C {
    initialize {
    variable CVar
    const CVar "My class variable"
    }
    method foo {} { # reads the constant
    classvariable CVar
    puts $CVar
    }
    method bar {} { # tries to set the constant
    classvariable CVar
    set CVar "other value"
    }
    }
    ::C
    % C create o
    ::o
    % o foo
    My class variable
    % o bar
    can't set "CVar": variable is a constant


    Regards
    --
    Emiliano
    --- Synchronet 3.21a-Linux NewsLink 1.2