[Prev][Next][Index][Thread]

Re: Dylan Object Protection



On Tue, 13 Mar 2001 21:30:02 -0500 (EST), Bruce Hoult <bruce@hoult.org>
wrote:

> In article <dJzr6.1179$xU.61669849@news.xtra.co.nz>, "merridy" 
> <merridy@xtra.co.nz> wrote:
> 
> > Hi there,
> > I was wondering if anyone knew...
> > How well does Dylan allow you to control access to the data fields (and
> > perhaps methods) within objects? Can you define private/public/protected
> > data? Who can access them?  Friends?
> 
> Dylan does not have anything like private/public/protected at the class 
> level.  Protection in Dylan is done by exporting or not exporting things 
> from the module.
> 
> When you export a class from a module it does *not* automatically export 
> method and field names (to be precise using Dylan terminology: it does 
> not export slot getters and setters).  You can individually choose to 
> export or not export the accessors for any slot -- and you can export 
> the ability to read a slot (the "getter") without exporting the ability 
> to assign to the slot (the "setter").  You can even export the ability 
> to assign to the slot without exporting the ability to *read* it, if 
> you're that perverse.
> 
> Anything that is in the same module has full and total access to the 
> class.  Effectively, everything in the same module is a "friend".

So in order to achieve Java kinds of access in Dylan you might do:

(1) "private"

Either put the class into its own module, and don't export things you wish
to be private. Or, more commonly in Dylan, do as (2) but use a naming
convention, eg prefixing by "%" to denote "internal" fields.

(2) "package" (this is the default in Java and has no keyword)

Put the class into a module containing other classes that want access, and
don't export things from the module that you wish to have this kind of
access.

(3) "protected"

Put the class into a module containing other classes that want access, and
export things from a special module for extending protocols involving this
class. eg a module called "foo-internals".

(4) "public"

Put the class into a module. Export the things that you want to be public
from the module.

However, there are some more interesting things you can do with modules eg
in order to share interfaces between implementations and clients. See the
"Programming in Dylan" book.

__Jason


References: