View · Search · Index
No registered users in community xowiki
in last 10 minutes

RE: Private methods (was [Xotcl] build problem on solaris)

From: Schofield, Bryan \(GE Transportation\) <"Schofield,>
Date: Tue, 27 Jul 2004 14:54:29 -0400

> -----Original Message-----
> From: Aamer Akhter [mailto:aakhter_at_gmail.com]
> Sent: Tuesday, July 27, 2004 1:44 PM
> To: Schofield, Bryan (GE Transportation)
> Cc: xotcl_at_alice.wu-wien.ac.at
> Subject: Re: [Xotcl] build problem on solaris

[snip snip snip]
>
> One thing that popped into my mind was
> how to do private variables and methods (i think this can be done via
> the filters, but i haven't exactly figured them out yet)
>
> Thank-you to the developers for maintianing xotcl.

Here is a quick hack example showing how to privatize a method. The instproc "priv" simply registers a method name to be considered private. Then a filter checks to see if the a method being called from somewhere outside of the object has been registered; If so, it throws. There is a lot of refinement that could be done, but at least this shows how a filter could be used to provide levels of protection. Making variables private is a little trickier. That might require the overloading variable mutators like "set", "lappend", "append" & so on. But one could make all thoses method private and then you've essentially made all variable private. Then use "parameters" for publicly accessible data, which is probably better any way since you are not allowing outsiders to directly change a variable, but rather providing them an interface to get & set the value of some piece of data.

-- bryan

---
package require XOTcl
namespace import ::xotcl::*
Class Foo
Foo instproc init {args} {
   my array set priv {}
   my priv bar
}
Foo instproc bar {} {
   puts "[self] BAR!"
}
Foo instproc ack {} {
   my bar
   puts "[self] ACK!"
}
Foo instproc priv {method} {
   my set priv($method) 1
}
Foo instproc checkAccess {args} {
   puts "check access [self] [self calledproc] called from [self callingproc]"
   if {[my exists priv([self calledproc])] && ([self callingproc] eq "")} {
      return -code error "[self calledproc] is private"
   }
   next
}
Foo instfilter checkAccess
Foo f
f ack
# This next line will generate an error.
f bar