diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-07-18 17:53:37 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-07-18 17:53:37 -0400 |
| commit | 7e0e38bd155fab76fa3b6776f9184611d044903d (patch) | |
| tree | 9cbd13e8aae249cb97b1dd2b22a084817093890c /docs/doc/namespace.html | |
| parent | bbecd676b7c127fead3ef172bbae3ddf2fb7f19a (diff) | |
Finish lexical scope documentation
Diffstat (limited to 'docs/doc/namespace.html')
| -rw-r--r-- | docs/doc/namespace.html | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/docs/doc/namespace.html b/docs/doc/namespace.html index b8332eae..9ea2f214 100644 --- a/docs/doc/namespace.html +++ b/docs/doc/namespace.html @@ -18,7 +18,7 @@ </span><span class='Function'>DoThing</span> <span class='Gets'>←</span> <span class='String'>"TODO"</span><span class='Modifier2'>⊸</span><span class='Function'>!</span> </pre> <h2 id="uses">Uses</h2> -<p>The features of namespaces that make them useful in BQN programming are encapsulation and mutability. But these are exactly the same features that <a href="https://en.wikipedia.org/wiki/Closure_(computer_programming)">closures</a> provide! In fact a namespace is not much more than a closure with a name lookup system. Consequently namespaces don't really expand the basic functionality of the language, but just make it easier to use.</p> +<p>The features of namespaces that make them useful in BQN programming are encapsulation and mutability. But these are exactly the same features that <a href="lexical.html#closures">closures</a> provide! In fact a namespace is not much more than a closure with a name lookup system. Consequently namespaces don't really expand the basic functionality of the language, but just make it easier to use.</p> <p>Namespaces improve encapsulation by allowing many values to be exported at once. With only one way to call them, functions and modifiers aren't such a good way to define a large part of a program. With a namespace you can define lots of things and expose exactly the ones you want to the rest of the world. For example, it's typical for files to define namespaces. A reader can see the exported values just by searching for <code><span class='Gets'>⇐</span></code>, and if you're nice, you might declare them all at the beginning of the file. Careful use of exports can guarantee that potentially dangerous functions are used correctly: if it's only valid to call function <code><span class='Function'>B</span></code> after function <code><span class='Function'>A</span></code> has been called, export <code><span class='Function'>AB</span><span class='Gets'>⇐</span><span class='Brace'>{</span><span class='Function'>A</span><span class='Value'>𝕩</span><span class='Separator'>⋄</span><span class='Function'>B</span><span class='Value'>𝕩</span><span class='Brace'>}</span></code> and don't export <code><span class='Function'>B</span></code>.</p> <p>Mutability means that the behavior of one namespace can change over the course of the program. Mutability is often a liability, so make sure you really need it before leaning too heavily on this property. While there's no way to tell from the outside that a particular namespace is mutable, you can tell it isn't if the source code doesn't contain <code><span class='Gets'>↩</span></code>, as this is the only way it can modify the variables it contains.</p> <p>A namespace that makes use of mutability is essentially an object: a collection of state along with operations that act on it. <a href="oop.html">Object-oriented programming</a> is the other major use of namespaces. Contrary to the name, there's never a need to orient your programming around objects, and it's perfectly fine to use an object here or there when you need to, for instance to build a mutable queue of values.</p> |
