|
| 1 | + target: reference/standard_lib |
| 2 | + template: reference |
| 3 | + title: MoonScript v0.2.0 - Standard Library |
| 4 | +-- |
| 5 | + |
| 6 | +The MoonScript installation comes with a small kernel of functions that can be |
| 7 | +used to perform various common things. |
| 8 | + |
| 9 | +The entire library is currently contained in a single object. We can bring this |
| 10 | +`moon` object into scope by requiring `"moon"`. |
| 11 | + |
| 12 | + require "moon" |
| 13 | + -- `moon.p` is the debug printer |
| 14 | + moon.p { hello: "world" } |
| 15 | + |
| 16 | +If you prefer to just inject all of the functions into the current scope, you |
| 17 | +can require `"moon.all"` instead. The following has the same effect as above: |
| 18 | + |
| 19 | + require "moon.all" |
| 20 | + p { hello: "world" } |
| 21 | + |
| 22 | +All of the functions are compatible with Lua in addition to MoonScript, but |
| 23 | +some of them only make sense in the context of MoonScript. |
| 24 | + |
| 25 | + |
| 26 | +# MoonScript Standard Library |
| 27 | + |
| 28 | +This is an overview of all the included functions. |
| 29 | +All of the examples assume that the standard library has been included with |
| 30 | +`require "moon.all"`. |
| 31 | + |
| 32 | +## Printing Functions |
| 33 | + |
| 34 | +### `p(arg)` |
| 35 | + |
| 36 | +Prints a formatted version of an object. Excellent for introspecting the contents |
| 37 | +of a table. |
| 38 | + |
| 39 | + |
| 40 | +## Table Functions |
| 41 | + |
| 42 | +### `run_with_scope(fn, scope, [args...])` |
| 43 | + |
| 44 | +Mutates the environment of function `fn` and runs the function with any extra |
| 45 | +args in `args...`. Returns the result of the function. |
| 46 | + |
| 47 | +The environment of the function is set to a new table whose metatable will use |
| 48 | +`scope` to look up values. `scope` must be a table. If `scope` does not have an |
| 49 | +entry for a value, it will fall back on the original environment. |
| 50 | + |
| 51 | + my_env = { |
| 52 | + secret_function: -> print "shhh this is secret" |
| 53 | + say_hi: -> print "hi there!" |
| 54 | + } |
| 55 | + |
| 56 | + say_hi = -> print "I am a closure" |
| 57 | + |
| 58 | + fn = -> |
| 59 | + secret_function! |
| 60 | + say_hi! |
| 61 | + |
| 62 | + run_with_scope fn, my_env |
| 63 | + |
| 64 | + |
| 65 | +Note that any closure values will always take precedence against global name |
| 66 | +lookups in the environment. In the example above, the `say_hi` in the |
| 67 | +environment has been shadowed by the local variable `say_hi`. |
| 68 | + |
| 69 | +### `defaultbl([tbl,] fn)` |
| 70 | + |
| 71 | +Sets the `__index` of table `tbl` to use the function `fn` to generate table |
| 72 | +values when a missing key is looked up. |
| 73 | + |
| 74 | +### `extend` |
| 75 | +### `copy` |
| 76 | + |
| 77 | +## Class/Object Functions |
| 78 | + |
| 79 | +### `bind_methods` |
| 80 | +### `mixin` |
| 81 | +### `mixin_object` |
| 82 | +### `mixin_table` |
| 83 | + |
| 84 | +## Misc Functions |
| 85 | + |
| 86 | +### `fold` |
| 87 | + |
| 88 | +## Debug Functions |
| 89 | + |
| 90 | +### `debug.upvalue` |
0 commit comments