• A TclOO question (for experts?)

    From Helmut Giese@21:1/5 to All on Mon Apr 7 18:34:28 2025
    Hello out there,
    look at the following sample:
    ---
    catch {Sample destroy}

    oo::class create Sample {
    variable classVar ;# automatically exists in every instance
    constructor {someVar} {
    my variable objVar
    set classVar $someVar
    set objVar 99
    }
    destructor {
    my variable objVar classVar
    puts "classVar: $classVar, objVar: $objVar"
    }
    }

    Sample create s1 100
    Sample create s2 200
    s1 destroy ;# -> classVar: 100, objVar: 99
    s2 destroy ;# -> classVar: 200, objVar: 99
    ---
    My question is: What happens, if I add 'classVar', although not
    needed, to a 'my variable' statement - like I did in the destructor
    above. Is it just superfluous or can it have consequences in a more
    complex situation than the above example.
    Thank you for any enlightenment
    Helmut

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From rene@21:1/5 to All on Tue Apr 8 06:01:42 2025
    Hello Helmut

    AFAIK the varaible statement in the class context will fill a list.
    This list is used to "upvar" your variables in the context of newly
    created methods.
    But beware. When you create new method in the object context with
    oo::objdefine the list is not used. This means you have to manually call
    the variable command.

    HTH
    rene

    --

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Helmut Giese@21:1/5 to All on Wed Apr 9 15:59:35 2025
    Hello Rene,
    thanks for your answer , it was really helpful
    Helmut

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From aotto1968@21:1/5 to Helmut Giese on Fri Apr 11 19:08:27 2025
    On 07.04.25 18:34, Helmut Giese wrote:
    Hello out there,
    look at the following sample:
    ---
    catch {Sample destroy}

    oo::class create Sample {
    variable classVar ;# automatically exists in every instance
    constructor {someVar} {
    my variable objVar
    set classVar $someVar
    set objVar 99
    }
    destructor {
    my variable objVar classVar
    puts "classVar: $classVar, objVar: $objVar"
    }
    }

    Sample create s1 100
    Sample create s2 200
    s1 destroy ;# -> classVar: 100, objVar: 99
    s2 destroy ;# -> classVar: 200, objVar: 99
    ---
    My question is: What happens, if I add 'classVar', although not
    needed, to a 'my variable' statement - like I did in the destructor
    above. Is it just superfluous or can it have consequences in a more
    complex situation than the above example.
    Thank you for any enlightenment
    Helmut

    a very good source for all kind of programming questions is the KI
    for example the GROK from X(twitter)

    https://x.com/i/grok

    it is really impressive, here is your answer:

    https://x.com/i/grok/share/Wo4XKUycnscf3WNTbpMoms93f

    mfg

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Helmut Giese@21:1/5 to All on Fri Apr 11 21:35:07 2025
    Hi aotto.
    that's really cool: a complete, detailed analysis which even I
    understood. Many thanks for this link
    Helmut

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ralf Fassel@21:1/5 to All on Mon Apr 14 11:08:05 2025
    * Helmut Giese <hgiese@ratiosoft.com>
    | | → https://x.com/i/grok/share/Wo4XKUycnscf3WNTbpMoms93f
    | that's really cool: a complete, detailed analysis which even I
    | understood. Many thanks for this link

    The text created by the AI contains a subtle error: it makes a
    difference between "variable foo" at class level and "my variable foo"
    at method level as the former being a "class-wide" variable (shared
    across instances, 'static' in C++, 'common' in itcl) and the latter "instance-specific" ones (specific to one object):

    [Complex situations, p2]
    ...
    ["my variable classVar" in method without previous "variable classVar" at class level]
    ...
    In this case, classVar becomes an instance-specific variable, not a
    class-wide one, which could lead to unexpected behavior if the
    intent was to share classVar across instances.

    As far as I understand it, this is not the case. Also the code you
    posted cleary shows that classVar is *not* shared between instances
    (each object has it's own value).

    Maybe someone with more knowlegde on this can share some insight?

    R'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ashok@21:1/5 to abu on Mon Apr 14 21:51:55 2025
    Not seen Grok's example but Tcl 9 does have class variables. See https://www.tcl-lang.org/man/tcl9.0/TclCmd/classvariable.html.


    On 4/14/2025 9:35 PM, abu wrote:
    Grok was very clever explaining the current code, but it was wrong
    trying to depict a more complex case.

    "classVar" is not a class-variable (despite its name).
    Simply Tcloo has not (yet) a concept of class-variables. All the
    variables are instance-variables,

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