XOTcl and namespace 
Author Message
 XOTcl and namespace

I have a small script

##script start

package require XOTcl
namespace import xotcl::*

Class Myclass
Myclass instproc init {} {
        my requireNamespace
        pack [entry .en -textvariable [self]::myvar]
        pack [label .lab]
        pack [button .bu -text start -command ".lab configure -text
$[self]::myvar "]  

Quote:
}

##case 1
Myclass myclass
##case 2
Myclass new

## script end

In case 1 the script run as expected.
In case 2 I get an error <<can't read "xotcl::__" no such variable>>

What's wrong, if the objectname is generated automatic.

Regards
Manfred



Thu, 11 Dec 2008 18:22:32 GMT  
 XOTcl and namespace

Manfred Stelzhammer a crit :

Quote:
> I have a small script
>    pack [button .bu -text start -command ".lab configure -text
> $[self]::myvar "]
> ##case 2
> Myclass new
> In case 2 I get an error <<can't read "xotcl::__" no such variable>>

The object name in this case is xotcl::__#1.
The hash symbol seems not to be allowed in a variable name.
Add this proc to your program and it will work :

Myclass proc new args {
eval my [my autoname myclass] $args

Quote:
}

(taken from the XOTcl documentation, I just changed the class name)

Regards,

Stphane



Fri, 12 Dec 2008 02:47:23 GMT  
 XOTcl and namespace

Quote:

> The object name in this case is xotcl::__#1.
> The hash symbol seems not to be allowed in a variable name.

interesting; i have not seen any limitations on the variable names in
tk in the documentation.
Is this considered a bug or a feature in tk?


Fri, 12 Dec 2008 16:23:33 GMT  
 XOTcl and namespace

Quote:

> interesting; i have not seen any limitations on the variable names in
> tk in the documentation.

There aren't any. It's not a Tk limitation. It's a limitation on the set
of characters supported by Tcl's $var substitution syntax, and it has
been there for at least a decade; I don't know when the $var syntax was
originally introduced, so I can't comment further without gnosticating
over the Tcl changes file. :-) That makes the problem something that is
actually in the user's script, and not in any library.

Quote:
> Is this considered a bug or a feature in tk?

It's considered to be "the way it is". I suppose that makes it a
feature, except it isn't really something that we particularly feel
proud of. :-)

The work-around for this is to use single-arg [set] syntax, which has
*no* limitations on variable names (well, not quite[*]; none that matter
here is perhaps a fairer thing to say). Like this:

   pack [button .bu -text start -command \
       ".lab configure -text \[set [self]::myvar]"]

The backslash is needed here because the expansion of the [set] would
otherwise happen at button creation time (unlike with the $ version).

Donal.
[* There are limitations on things that look like arrays and on colon
    handling, but these are hardly ever problems in practice. ]



Fri, 12 Dec 2008 17:12:36 GMT  
 XOTcl and namespace

Quote:


> > The object name in this case is xotcl::__#1.
> > The hash symbol seems not to be allowed in a variable name.

> interesting; i have not seen any limitations on the variable names in
> tk in the documentation.
> Is this considered a bug or a feature in tk?

See rule #7 from the endekalogue/tcl man page (the following from
http://www.tcl.tk/man/tcl8.4/TclCmd/Tcl.htm#M11), ${xotcl::__#1} should
work just fine:

[7] Variable substitution.
    If a word contains a dollar-sign (``$'') then Tcl performs variable
substitution: the dollar-sign and the following characters are replaced in
the word by the value of a variable. Variable substitution may take any of
the following forms:

    $name
        Name is the name of a scalar variable; the name is a sequence of
one or more characters that are a letter, digit, underscore, or namespace
separators (two or more colons).

    $name(index)
        Name gives the name of an array variable and index gives the name
of an element within that array. Name must contain only letters, digits,
underscores, and namespace separators, and may be an empty string. Command
substitutions, variable substitutions, and backslash substitutions are
performed on the characters of index.

    ${name}
        Name is the name of a scalar variable. It may contain any
characters whatsoever except for close braces.

    There may be any number of variable substitutions in a single word.
Variable substitution is not performed on words enclosed in braces.

Michael



Sat, 13 Dec 2008 02:18:30 GMT  
 XOTcl and namespace
Michael A. Cleverly schrieb:

Quote:
>     There may be any number of variable substitutions in a single word.
> Variable substitution is not performed on words enclosed in braces.

> Michael

well, i know this, but how can one force tk to "put these braces around
the variable name", where ever it does the subst?


Sun, 14 Dec 2008 08:20:45 GMT  
 XOTcl and namespace

Quote:

> Michael A. Cleverly schrieb:
> >     There may be any number of variable substitutions in a single word.
> > Variable substitution is not performed on words enclosed in braces.

> > Michael

> well, i know this, but how can one force tk to "put these braces around
> the variable name", where ever it does the subst?

You can't.  But you could use [set var] instead of $var.

Michael



Sun, 14 Dec 2008 08:47:25 GMT  
 XOTcl and namespace

Michael A. Cleverly schrieb:
 > You can't.  But you could use [set var] instead of $var.

Quote:

> Michael

Thanks, got it. the problem was not the line with the textvariable (all
names are allowed) but the command of the button. The following command
does the trick with the curly braces:

pack [button .bu -text start -command ".lab configure -text
\${[self]::myvar}"]



Sun, 14 Dec 2008 09:54:45 GMT  
 XOTcl and namespace

Quote:

> Michael A. Cleverly schrieb:
>  > You can't.  But you could use [set var] instead of $var.

>>Michael

> Thanks, got it. the problem was not the line with the textvariable (all
> names are allowed) but the command of the button. The following command
> does the trick with the curly braces:

> pack [button .bu -text start -command ".lab configure -text
> \${[self]::myvar}"]

Personally I'd never recommend creating a binding that depends on a
variable like that with quoting like that. Why? Because I had to read it
about 5 times before I really understood what you are trying to do (and
not having followed the thread and seen the context, I'm still not sure
why all the quoting is necessary).

Can't you write a proc or method inside of [self] that doesn't have to
fully qualify myvar? I'm sure this isn't the right xotcl syntax, but I
think the spirit of the code is correct:

   button .bu -command [list config_lab .labe myvar]
   proc [self]::config_lab {w} {
     variable myvar
     $w configure -text $myvar
   }

Using pure namespaces, doesn't the following also do what you want
(assuming [self] is the same as the current namespace)?

   button .bu -command [namespace code {
     .lab configure -text $myvar
   }

--
Bryan Oakley
http://www.tclscripting.com



Sun, 14 Dec 2008 11:26:05 GMT  
 XOTcl and namespace

Bryan Oakley schrieb:

Quote:
> Personally I'd never recommend creating a binding that depends on a
> variable like that with quoting like that. Why? Because I had to read it
> about 5 times before I really understood what you are trying to do (and
> not having followed the thread and seen the context, I'm still not sure
> why all the quoting is necessary).

it was not my code; i was trying to help out and misread one of the
former replies in a way, that tk performs somewhere in the entry widget
a subst and was confused that i could not find a subst in the tk
source...

i fully agree that the construct is hard to read. This is  due to the
fact that the namespace returned by self contains a character that is
not valid in ab unbraced dollar substitution.

the original command contains "... $[self]::myvar ...", which is indeed
tricky, since the $ is not evaluated during command registration, but
in the callback (the dollar substitution stops due to the bracket)

proc x {} {return "_#1"}
% set y $[x]::name
$_#1::name

when the callback is evaluated, the hash sign causes troubles. i would
rewrite the line as

    pack [button .bu -text start -command [list [self] eval {.lab
configure -text $myvar}]]

or either as a separate method

  Myclass instproc start_cmd {} {
    my instvar myvar
    .lab configure -text $myvar
  }
  pack [button .bu -text start -command [list [self] start_cmd]]

both approaches work with arbitrary namespaces without to much magic.



Sun, 14 Dec 2008 17:04:39 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Namespaces in Scheme vs. Namespaces in CL.

2. To namespace or not to namespace

3. Problem with using a Namespace variable in another namespace function

4. Importing a Namespace Failing to Import Namespace??

5. Namespace of a namespace question.

6. Namespaces in Scheme vs. Namespaces in CL.

7. BUG/PROBLEM: Tcl8.0b1 namespace children/delete {} in a namespace

8. namespaces and objects (namespace ::<classname>)

9. XOTCL

10. Getting the name of base class object in XOTCL

11. xotcl 1.3.9

12. xotcl

 

 
Powered by phpBB® Forum Software