• Re: Are class consts possible?

    From Rich@21:1/5 to Mark Summerfield on Fri Jun 20 13:28:13 2025
    Mark Summerfield <[email protected]> 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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to Mark Summerfield on Fri Jun 20 20:28:25 2025
    Mark Summerfield <[email protected]> wrote:
    On Fri, 20 Jun 2025 13:28:13 -0000 (UTC), Rich wrote:

    Mark Summerfield <[email protected]> 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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Emiliano@21:1/5 to Mark Summerfield on Sat Jun 21 14:03:33 2025
    On Fri, 20 Jun 2025 07:58:37 -0000 (UTC)
    Mark Summerfield <[email protected]> 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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Emiliano@21:1/5 to Emiliano on Sat Jun 21 18:16:15 2025
    On Sat, 21 Jun 2025 14:03:33 -0300
    Emiliano <[email protected]d> wrote:

    On Fri, 20 Jun 2025 07:58:37 -0000 (UTC)
    Mark Summerfield <[email protected]> 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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)