I am trying to learn xotcl classes and their use from the xotcl- core.pdf examples. Most of the code examples look like they are being entered into a console while I am trying to write tcl files and then run them.
Here's the class definition: ------------------------------------------------------------ package require XOTcl ::xotcl::Class Stack Stack instproc init {} { # Constructor my instvar things set things "" }
Stack instproc push {thing} { my instvar things set things [concat [list $thing] $things] return $thing }
Stack instproc pop {} { my instvar things set top [lindex $things 0] set things [lrange $things 1 end] return $top }
--------------------------------------------------------- How do I access the class methods(i guess they are called) from within the tcl file? The examples show this: -------------------------------------------------------- # Create Object s1 of class Stack % Stack s1 ::s1 % s1 push a a % s1 push b b % s1 push c c % s1 pop c % s1 pop b # Delete object s1 s1 destroy --------------------------------------------------------
I have tried this: set s1 [Stack] puts $s1
Which outputs: ::Stack
But I'm not sure where to go from here... Thank you.
On 7 May 2008, at 20:54, Matthew Smith wrote:
How do I access the class methods(i guess they are called) from within the tcl file? The examples show this:
# Create Object s1 of class Stack % Stack s1 ::s1 % s1 push a a % s1 push b b % s1 push c c % s1 pop c % s1 pop b # Delete object s1 s1 destroy
I have tried this: set s1 [Stack]
Either this:
set s1 [Stack new]
or
set s1 [Stack s1]
The first version gives you an automatically named object, which is probably what you usually want to do. You can also explicitly name objects with the latter version (something you cannot do with Java and stuff). Mostly you wont need to do this, but there are situations when it's useful.
Thank you for the help.
Now I am getting another error I don't understand.
invalid command name "my".
It seems like it is not getting to use XOTcl commands. Here is what I have: ---------------------------------------------------------- package require XOTcl ::xotcl::Class Stack Stack instproc init {} { # Constructor my instvar things set things "" } Stack instproc push {thing} { my instvar things set things [concat [list $thing] $things] return $thing } Stack instproc pop {} { my instvar things set top [lindex $things 0] set things [lrange $things 1 end] return $top }
set s1 [Stack new] ----------------------------------------------------------
On Wed, May 7, 2008 at 1:01 PM, Kristoffer Lawson setok@scred.com wrote:
On 7 May 2008, at 20:54, Matthew Smith wrote:
How do I access the class methods(i guess they are called) from within the tcl file? The examples show this:
# Create Object s1 of class Stack % Stack s1 ::s1 % s1 push a a % s1 push b b % s1 push c c % s1 pop c % s1 pop b # Delete object s1 s1 destroy
I have tried this: set s1 [Stack]
Either this:
set s1 [Stack new]
or
set s1 [Stack s1]
The first version gives you an automatically named object, which is probably what you usually want to do. You can also explicitly name objects with the latter version (something you cannot do with Java and stuff). Mostly you wont need to do this, but there are situations when it's useful.
/ http://www.scred.com/ / http://www.fishpool.com/~setok/<http://www.fishpool.com/%7Esetok/>
On 7 May 2008, at 21:09, Matthew Smith wrote:
Thank you for the help.
Now I am getting another error I don't understand.
invalid command name "my".
"my" is probably in the xotcl namespace. When I'm coding with XOTcl I always start with the following:
package require XOTcl namespace import xotcl::*
Then I have direct access to all XOTcl commands:
Class Stack
Stack instproc init {} { my instvar things # ... }
Makes sense. Thank you for the help.
On Wed, May 7, 2008 at 1:15 PM, Kristoffer Lawson setok@scred.com wrote:
On 7 May 2008, at 21:09, Matthew Smith wrote:
Thank you for the help.
Now I am getting another error I don't understand.
invalid command name "my".
"my" is probably in the xotcl namespace. When I'm coding with XOTcl I always start with the following:
package require XOTcl namespace import xotcl::*
Then I have direct access to all XOTcl commands:
Class Stack
Stack instproc init {} { my instvar things # ...
}
/ http://www.scred.com/ / http://www.fishpool.com/~setok/<http://www.fishpool.com/%7Esetok/>
On 7 May 2008, at 21:25, Matthew Smith wrote:
How do I call the methods like push and pop?
Assuming:
set s1 [Stack new]
$s1 push foo $s1 pull
Matthew Smith schrieb:
How do I call the methods like push and pop?
Let us assume, you have a class "Stack" defined and the class stack contains definitions for the methods (instprocs) "push" and "pop". The class provides these methods for ins instances. Therefore, in order to invoke these methods, you will need an instance of class Stack (an object of the type Stack). One can create such an instances (e.g. "s1") with the following command
Stack s1
Once the object exists, the methods "push" and "pop" can be called via s1 push a
or
puts [s1 pop]
So, the first word of the Tcl command refers to the object (the instance of class stack) and the second word refers to the method. If there are more words, these are passed as arguments to the method.
Hope, this helps -gustaf neumann
PS: The xotcl-core tutorial ist for the OpenACS environment. There, it is not necessary to include the "package require XOTcl" and the "namespace import ::xotcl::*". You need these commands in case you use XOTcl in a tclsh.
Thank you, Gustaf. Makes sense.
Is there anyway to call a method besides doing a "puts".
Putting in: s1 push a results in an error, however I can do: puts [s1 pop]
I would think that there would be a way to call a method without outputiing. Is there one?
Matthew Smith schrieb:
Thank you, Gustaf. Makes sense.
Is there anyway to call a method besides doing a "puts".
sure.
Putting in: s1 push a results in an error, however I can do:
what is the error? this indicates that you have not defined the method push correctly.
puts [s1 pop]
I would think that there would be a way to call a method without outputiing. Is there one?
sure:
set a [s1 pop]
The method pop returns a value. The question is, what do you want to do in your application with the value. Technically, "s1" is a Tcl command (which happens to be an XOTcl object) that returns a value. By placing the tcl command between square braces, you will be performing "command substitution", which means "replace the command with the result of the command" during evaluation. Same mechanism as
set x [expr {1+2}]
or
puts [expr {1+2}]
hope this helps
-gustaf neumann
Putting in: s1 push a results in an error, however I can do:
what is the error? this indicates that you have not defined the method push correctly
I get: invalid command name "s1" for: s1 push 1 I get the same for: xotcl::s1 push 1
On Thu, May 8, 2008 at 2:27 AM, Gustaf Neumann neumann@wu-wien.ac.at wrote:
Matthew Smith schrieb:
Thank you, Gustaf. Makes sense.
Is there anyway to call a method besides doing a "puts".
sure.
Putting in: s1 push a results in an error, however I can do:
what is the error? this indicates that you have not defined the method push correctly.
puts [s1 pop]
I would think that there would be a way to call a method without outputiing. Is there one?
sure:
set a [s1 pop]
The method pop returns a value. The question is, what do you want to do in your application with the value. Technically, "s1" is a Tcl command (which happens to be an XOTcl object) that returns a value. By placing the tcl command between square braces, you will be performing "command substitution", which means "replace the command with the result of the command" during evaluation. Same mechanism as
set x [expr {1+2}]
or
puts [expr {1+2}]
hope this helps
-gustaf neumann
Matthew Smith schrieb:
Putting in: s1 push a results in an error, however I can do:
what is the error? this indicates that you have not defined the method push correctly
I get: invalid command name "s1"
this indicates, that you have not created an object s1. Your program should look like the following.....
hope this helps -gustaf neumann
=========================================== package require XOTcl; namespace import ::xotcl::*
Class Stack Stack instproc init {} { my instvar things set things "" } Stack instproc push {thing} { my instvar things set things [concat [list $thing] $things] return $thing } Stack instproc pop {} { my instvar things set top [lindex $things 0] set things [lrange $things 1 end] return $top }
set s1 [Stack new]
s1 push a s1 push b
set x [s1 pop] puts "The popped value is $x" ===========================================
I copy and pasted what you posted, and still get the error: invalid command name "s1"
On Thu, May 8, 2008 at 10:59 AM, Gustaf Neumann neumann@wu-wien.ac.at wrote:
Matthew Smith schrieb:
Putting in: s1 push a results in an error, however I can do:
what is the error? this indicates that you have not defined the method push correctly
I get: invalid command name "s1"
this indicates, that you have not created an object s1. Your program should look like the following.....
hope this helps -gustaf neumann
=========================================== package require XOTcl; namespace import ::xotcl::*
Class Stack Stack instproc init {} { my instvar things set things "" } Stack instproc push {thing} { my instvar things set things [concat [list $thing] $things] return $thing } Stack instproc pop {} { my instvar things set top [lindex $things 0] set things [lrange $things 1 end] return $top }
set s1 [Stack new]
s1 push a s1 push b
set x [s1 pop] puts "The popped value is $x" ===========================================
That got it. Thank you.
On Thu, May 8, 2008 at 11:14 AM, Vasiljevic Zoran zv@archiware.com wrote:
On 08.05.2008, at 18:02, Matthew Smith wrote:
set s1 [Stack new]
s1 push a s1 push b
set x [s1 pop] puts "The popped value is $x"
Try this:
Stack s1
s1 push a s1 push b
set x [s1 pop] puts "The popped value is $x"
Matthew Smith schrieb:
I copy and pasted what you posted, and still get the error: invalid command name "s1"
sorry, my mistake. i did too much cur&paste from your earlier scripts. The version below should work
-gustaf neumann
===============================
package require XOTcl; namespace import ::xotcl::*
Class Stack Stack instproc init {} { my instvar things set things "" } Stack instproc push {thing} { my instvar things set things [concat [list $thing] $things] return $thing } Stack instproc pop {} { my instvar things set top [lindex $things 0] set things [lrange $things 1 end] return $top }
# using unnamed object set s1 [Stack new]
$s1 push a $s1 push b
set x [$s1 pop] puts "The popped value is $x"
# using named object
Stack s1
s1 push c s1 push d
set x [s1 pop] puts "The popped value is $x"