Namespaces

32 views
Skip to first unread message

Bruno Adam

unread,
Dec 12, 2016, 6:12:04 PM12/12/16
to Sleep Developers
Hello,

I would like to know if it's possible to use namespaces with Sleep? I would like to avoid name collisions when I include a lot a sl files.

Thanks,

Bruno

Raphael Mudge

unread,
Dec 12, 2016, 6:17:44 PM12/12/16
to sleep-de...@googlegroups.com
Hi Bruno,
Sleep doesn't have namespaces, but you can work without global
variables by making certain variables static to your various Sleep
closures/functions.

sub foo {
this('$x');
$x++;
println("\$x is now $x");
}

foo();
foo();
$x = 0;
foo();

This will output:

$x is now 1
$x is now 2
$x is now 3

I tend to make use of variables scoped to a function to reduce the
need for global vars in Sleep.

When a Sleep function calls another function, the child functions do
not inherit its variables, but you can explicitly pass values into a
function's scope using &let. The &lambda function will create a copy
of a function instance and let you install whatever you want into that
too.

Another option is to use fork which will essentially run a block of
Sleep code as its own thread within an isolated instance of the
interpreter. This helps provide some separation too.

Part 5.3 of the Sleep Manual shows how to combine some of Sleep's
built-in things to create different abstractions (e.g., classless
objects is a favorite of mine).

http://sleep.dashnine.org/manual/index.html

-- Raphael
> --
> --
> -----
> To post to this group, send email to sleep-de...@googlegroups.com
> To unsubscribe from this group, send email to
> sleep-develope...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/sleep-developers?hl=en
>
> The Sleep Scripting Project: http://sleep.dashnine.org/
> ---
> You received this message because you are subscribed to the Google Groups
> "Sleep Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sleep-develope...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages