From c15089ee1c0a186bf99cab57f8c2700b523c9f35 Mon Sep 17 00:00:00 2001
From: leaf corcoran \\
operator but
with no argument list provided.
my_object = {
value: 1000
- print: => print "the value:", @value
+ write: => print "the value:", @value
}
run_callback (func) ->
print "running callback..."
func!
- -- this will not work, the function has to no reference to my_object
- run_callback my_object.print
+ -- this will not work:
+ -- the function has to no reference to my_object
+ run_callback my_object.write
- -- function stub syntax lets us bundle the object into a new function
- run_callback my_object\print
+ -- function stub syntax
+ -- lets us bundle the object into a new function
+ run_callback my_object\write
## The Using Clause; Controlling Destructive Assignment
diff --git a/docs/reference_manual.html b/docs/reference_manual.html
index f83d279..22654bb 100644
--- a/docs/reference_manual.html
+++ b/docs/reference_manual.html
@@ -1,7 +1,7 @@
-
+
The same thing can be done with other block level statements like -conditionals. We can use indentation level to determine what +conditionals. We can use indentation level to determine what statement a value belongs to:
moonscript | lua |
|
|
local BackPack
BackPack = (function(_parent_0)
local _base_0 = {
@@ -1336,7 +1338,7 @@ will not be assigned locally.
module:
moonscript lua -- my_module.moon
-module "my_module", package.seeall
+module "my_module", package.seeall
export print_result
length = (x, y) -> math.sqrt x*x + y*y
@@ -1345,12 +1347,12 @@ module:
print "Length is ", length x, y
-- main.moon
-require "my_module"
+require "my_module"
my_module.print_result 4, 5 -- prints the result
print my_module.length 6, 7 -- errors, `length` not visible
-
module("my_module", package.seeall)
+
module("my_module", package.seeall)
local length
length = function(x, y)
return math.sqrt(x * x + y * y)
@@ -1358,7 +1360,7 @@ module:
print_result = function(x, y)
return print("Length is ", length(x, y))
end
-require("my_module")
+require("my_module")
my_module.print_result(4, 5)
print(my_module.length(6, 7))
@@ -1387,7 +1389,7 @@ to bind it to that table:
import \add from my_module
-print add(22) -- equivalent to calling my_module:get(22)
+print add(22) -- equivalent to calling my_module\get 22
local my_module = {
state = 100,
add = function(self, value)
@@ -1407,8 +1409,11 @@ unnecessary noise. A common solution to this is to pass a table in as an
argument which contains a collection of keys and values to overwrite. The
downside to this is that the constructor of this object must support this form.
-The with
block helps to alleviate this. It lets us use a bare function and
-index syntax in order to work with the object:
+The with
block helps to alleviate this. Within a with
block we can use a
+special statements that begin with either .
or \` which represent
+those operations applied to the object we are using
withon.
+
+For example, we work with a newly created object:
moonscript lua with Person!
.name = "Oswald"
@@ -1464,6 +1469,55 @@ it has been giving access to.
sister
})
+Function Stubs
+
+It is common to pass a function from an object around as a value, for example,
+passing an instance method into a function as a callback. If the function
+expects the object it is operating on as the first argument then you must
+somehow bundle that object with the function so it can be called properly.
+
+The function stub syntax is a shorthand for creating a new closure function
+that bundles both the object and function. This new function calls the wrapped
+function in the correct context of the object.
+
+Its syntax is the same as calling an instance method with the \
operator but
+with no argument list provided.
+
+moonscript lua my_object = {
+ value: 1000
+ write: => print "the value:", @value
+}
+
+run_callback (func) ->
+ print "running callback..."
+ func!
+
+-- this will not work:
+-- the function has to no reference to my_object
+run_callback my_object.write
+
+-- function stub syntax
+-- lets us bundle the object into a new function
+run_callback my_object\write
+
local my_object = {
+ value = 1000,
+ write = function(self)
+ return print("the value:", self.value)
+ end
+}
+run_callback(function(func)
+ print("running callback...")
+ return func()
+end)
+run_callback(my_object.write)
+run_callback((function()
+ local _base_0 = my_object
+ local _fn_0 = _base_0.write
+ return function(...)
+ return _fn_0(_base_0, ...)
+ end
+end)())
+
The Using Clause; Controlling Destructive Assignment
While lexical scoping can be a great help in reducing the complexity of the
@@ -1487,7 +1541,7 @@ the following snippet:
local my_func
my_func = function()
i = 10
- while i > 0 do
+ while i > 0 do
print(i)
i = i - 1
end
@@ -1553,8 +1607,8 @@ accessed, they just cant be modified:
Upon installing MoonScript, a moonscript
module is made available. The best
use of this module is making your Lua’s require function MoonScript aware.
-moonscript lua require "moonscript"
-
require("moonscript")
+moonscript lua require "moonscript"
+
require("moonscript")
After moonscript
is required, Lua’s package loader is updated to search for
.moon
files on any subsequent calls to require
. The search path for .moon
@@ -1615,8 +1669,8 @@ Lua code from MoonScript code.
Here is a quick example of how you would compile a MoonScript string to a Lua
String:
-moonscript lua require "moonscript.parse"
-require "moonscript.compile"
+moonscript lua require "moonscript.parse"
+require "moonscript.compile"
import parse, compile from moonscript
@@ -1632,8 +1686,8 @@ String:
-- our code is ready
print lua_code
-
require("moonscript.parse")
-require("moonscript.compile")
+
require("moonscript.parse")
+require("moonscript.compile")
local parse, compile = moonscript.parse, moonscript.compile
local moon_code = [[(-> print "hello world")!]]
local tree, err = parse.string(moon_code)
@@ -1720,7 +1774,7 @@ THE SOFTWARE.
-
+