log(error, ~"file not found: " + filename);
// Formatting the message using a format-string and #fmt
-log(error, #fmt("file not found: %s", filename));
+log(error, fmt!("file not found: %s", filename));
// Using the #error macro, that expands to the previous call.
-#error("file not found: %s", filename);
+error!("file not found: %s", filename);
~~~~
A `log` expression is *not evaluated* when logging at the specified
#macro([#apply[fn, [args, ...]], fn(args, ...)]);
~~~~~~~~
-In this case, the invocation `#apply[sum, 5, 8, 6]` expands to
+In this case, the invocation `apply!(sum, 5, 8, 6)` expands to
`sum(5,8,6)`. If `...` follows an expression (which need not be as
simple as a single identifier) in the input syntax, the matcher will expect an
arbitrary number of occurrences of the thing preceding it, and bind syntax to
#macro([#unzip_literals[[x, y], ...], [[x, ...], [y, ...]]]);
~~~~~~~~
-In this case, `#zip_literals[[1,2,3], [1,2,3]]` expands to
-`[[1,1],[2,2],[3,3]]`, and `#unzip_literals[[1,1], [2,2], [3,3]]`
+In this case, `zip_literals!([1,2,3], [1,2,3])` expands to
+`[[1,1],[2,2],[3,3]]`, and `unzip_literals!([1,1], [2,2], [3,3])`
expands to `[[1,2,3],[1,2,3]]`.
Macro expansion takes place outside-in: that is,
-`#unzip_literals[#zip_literals[[1,2,3],[1,2,3]]]` will fail because
+`unzip_literals!(zip_literals!([1,2,3],[1,2,3]))` will fail because
`unzip_literals` expects a list, not a macro invocation, as an argument.
The macro system currently has some limitations. It's not possible to
// Report the results as the games complete
for range(0, times) |round| {
let winner = result_from_game.recv();
- println(#fmt("%s wins round #%u", winner, round));
+ println(fmt!("%s wins round #%u", winner, round));
}
fn play_game(player1: ~str, player2: ~str) -> ~str {
at compile time.
~~~~
-io::println(#fmt("%s is %d", ~"the answer", 42));
+io::println(fmt!("%s is %d", ~"the answer", 42));
~~~~
`#fmt` supports most of the directives that [printf][pf] supports, but
compile-time.
~~~~
-io::println(#env("PATH"));
+io::println(env!("PATH"));
~~~~
# Control structures
~~~~
# fn get_error_string() -> ~str { ~"boo" }
-#warn("only %d seconds remaining", 10);
-#error("fatal: %s", get_error_string());
+warn!("only %d seconds remaining", 10);
+error!("fatal: %s", get_error_string());
~~~~
Because the macros `#debug`, `#warn`, and `#error` expand to calls to `log`,
fn call_closure_with_ten(b: fn(int)) { b(10); }
let captured_var = 20;
-let closure = |arg| println(#fmt("captured_var=%d, arg=%d", captured_var, arg));
+let closure = |arg| println(fmt!("captured_var=%d, arg=%d", captured_var, arg));
call_closure_with_ten(closure);
~~~~
# fn each(v: ~[int], op: fn(int)) {}
# fn do_some_work(i: int) { }
each(~[1, 2, 3], |n| {
- #debug("%i", n);
+ debug!("%i", n);
do_some_work(n);
});
~~~~
# fn each(v: ~[int], op: fn(int)) {}
# fn do_some_work(i: int) { }
do each(~[1, 2, 3]) |n| {
- #debug("%i", n);
+ debug!("%i", n);
do_some_work(n);
}
~~~~
import task::spawn;
do spawn() || {
- #debug("I'm a task, whatever");
+ debug!("I'm a task, whatever");
}
~~~~
~~~~
# import task::spawn;
do spawn {
- #debug("Kablam!");
+ debug!("Kablam!");
}
~~~~
fn as_hex(data: ~[u8]) -> ~str {
let mut acc = ~"";
- for data.each |byte| { acc += #fmt("%02x", byte as uint); }
+ for data.each |byte| { acc += fmt!("%02x", byte as uint); }
return acc;
}
return (x.tv_sec as u64) * 1000_000_u64 + (x.tv_usec as u64);
}
-# fn main() { assert #fmt("%?", unix_time_in_microseconds()) != ~""; }
+# fn main() { assert fmt!("%?", unix_time_in_microseconds()) != ~""; }
~~~~
The `#[nolink]` attribute indicates that there's no foreign library to
do spawn {
println(~"This executes in the child task.");
- println(#fmt("%d", some_value));
+ println(fmt!("%d", some_value));
}
~~~~