Destroy no more useful functions
[1/10] from: didier:jacquemart:libertysurf at: 15-Apr-2002 23:10
Hello.
I wrote a source file, containing several general purpose functions, called funcsrc.r
In a program, i then write
do %funcsrc.r
.... calling funcs
When my program ends, i want to destroy all the funcs included by funcsrc.r.
But writing unset 'func1, unset 'func2 ... is a bit boring and much unsure.
Has anybody an idea ?
[2/10] from: sunandadh:aol at: 16-Apr-2002 17:38
Didier:
> In a program, i then write
> do %funcsrc.r
> .... calling funcs
> When my program ends, i want to destroy all the funcs included by
funcsrc.r.
> But writing unset 'func1, unset 'func2 ... is a bit boring and much unsure.
> Has anybody an idea ?
I put all the functions in a source module into an object:
funcsrc.r contains:
funcsrc: make object! [
func1: func [] []
func2: func [] []
] ; object
This has three main advantages:
3. You are less likely to create a name clash with a Rebol function, or
another module you have included, because your names have two parts. So you
could have the functions:
funcsrc/print
funcscr/return
etc without clashing with the "global" 'print or 'return functions
2. Similarly, you can have "module-local" variables: accessible to any
function in the object, but not clashing in name with any variable declared
elsewhere:
funcsrc: make object! [
var1: 666
func1: func [] [print var1] ;; prints funcsrc/var1
func2: func [] [print var1] ;; ditto
] ; object
1. Finally, you can trash the object, and all its functions and variables in
one go:
unset 'funcsrc
You decide if that's funky or boring,
Sunanda.
[3/10] from: joel:neely:fedex at: 16-Apr-2002 16:42
Hi, Didier,
Here's my preferred approach...
Didier Jacquemart wrote:
> Hello.
> I wrote a source file, containing several general purpose functions,
<<quoted lines omitted: 6>>
> boring and much unsure.
> Has anybody an idea ?
Assuming that funcsrc.r contains a related collection of functions,
modify funcsrc.r so that it defines a single object having all of
those functions as members/methods.
REBOL [
...
]
make object! [
func1: func [...] [...]
func2: func [...] [...]
...
]
You can use this in the following manner:
fns: do %funcsrc.r
fns/func1 ...
fns/func2 ...
...
then, at the end of the useful life for the function library
unset 'foo
or
foo: none
or whatever.
-jn-
[4/10] from: louisaturk:eudoramail at: 16-Apr-2002 17:48
Sunanda,
Interesting. This is what I was thinking that recycle might do. I am
beginning to see more all the time that using objects can be very useful.
Thanks,
Louis
At 05:38 PM 4/16/2002 -0400, you wrote:
[5/10] from: sunandadh:aol at: 17-Apr-2002 10:55
Louis:
> Interesting. This is what I was thinking that recycle might do. I am
> beginning to see more all the time that using objects can be very useful.
I didn't invent the idea of using an object to encapsulate functions -- I got
it from someone on this list. And, truth-be-told, it would still be better if
the mythical Rebol 3.0 implemented a proper module-model.
But funcs-in-obs is a useful way to package an application. I tend to be
writing largish applications (6,000-12,000 lines of code, with a couple of
hundred functions). I tend to use this sort of template for where to put a
function:
appl-main.r -- the main guts of the application
appl-init.r -- start-up, user-identification etc
appl-rec.r -- error recovery and reporting
appl-serv -- service functions (application-specific subroutines)
appl-panels -- Vid panels
appl-dd -- data definitions (data objects)
appl-io -- (read/write/decrypt etc for most files)
And then I have a few modules that are cross-application:
debug.r -- useful debugging tools
gui-utils -- VID functions (eg slider style)
std-utils -- other useful functions (eg soundex, csv-->object etc)
patches.r -- fixes to Rebol functions
Deciding where to put a function is half the struggle in deciding if I need
it at all.
Sunanda.
[6/10] from: petr:krenzelok:trz:cz at: 17-Apr-2002 18:21
[SunandaDH--aol--com] wrote:
>And then I have a few modules that are cross-application:
>
>debug.r -- useful debugging tools
>gui-utils -- VID functions (eg slider style)
>std-utils -- other useful functions (eg soundex, csv-->object etc)
>patches.r -- fixes to Rebol functions
>
I just call them:
lib-sdf.r
lib-odbc.r
lib-fcgi.r
etc., to have all libs sorted together in a file manager :-)
-pekr-
[7/10] from: louisaturk:eudoramail at: 17-Apr-2002 13:03
Sunanda,
Even more interesting. Let me make sure I understand. Are you saying that
each one of your modules is packaged in an object? Would you have a really
short example of this that you might be willing to share?
Louis
At 10:55 AM 4/17/2002 -0400, you wrote:
[8/10] from: sunandadh:aol at: 17-Apr-2002 18:08
Louis:
> Even more interesting. Let me make sure I understand. Are you saying that
> each one of your modules is packaged in an object?
Exactly. Each physical source module has this standard format:
aaa....
mod-name: make object! [
bbb...
ccc...
] ;end of object
where:
aaa... is any global variables. I know I shouldn't have them, but I cut
myself some slack when coding at high speed
bbb... is any variables common to the object
ccc... is any functions within the object
> Would you have a really
> short example of this that you might be willing to share?
I don't have anything short enough to post on the list, but I'm sending you
an example direct. Anyone else want the same, just email me privately.
Sunanda.
[9/10] from: louisaturk:eudoramail at: 17-Apr-2002 18:18
Sunanda,
I received the example. I appreciate it very much. I'll study it
carefully. This list has really been a valuable learning experience for
me. Because of you and others on this list Rebol is becoming a very
valuable asset for me. There must be something very special about rebol to
have attracted so such great people.
Many thanks!
Louis
At 06:08 PM 4/17/2002 -0400, you wrote:
[10/10] from: louisaturk:eudoramail at: 17-Apr-2002 22:09
Petr,
Thanks, for the tip! I read most of your posts. You have a style of
writing that makes what you say interesting even when I don't yet
understand all that you are discussing. You help me learn.
Louis
At 06:21 PM 4/17/2002 +0200, you wrote:
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted