Update syntax for function arguments; tweak object system examples to
make mutable fields work.
This commit is contained in:
parent
5d0f9d92ff
commit
133b3d05b4
@ -1305,14 +1305,14 @@ An example function that accepts an alias parameter:
|
|||||||
@example
|
@example
|
||||||
type point3d = @{x: int, y: int, z: int@};
|
type point3d = @{x: int, y: int, z: int@};
|
||||||
|
|
||||||
fn extract_z(&point3d p) -> int @{
|
fn extract_z(p: &point3d) -> int @{
|
||||||
ret p.z;
|
ret p.z;
|
||||||
@}
|
@}
|
||||||
@end example
|
@end example
|
||||||
|
|
||||||
An example function that accepts an alias to a mutable value:
|
An example function that accepts an alias to a mutable value:
|
||||||
@example
|
@example
|
||||||
fn incr(& mutable int i) @{
|
fn incr(i: &mutable int) @{
|
||||||
i = i + 1;
|
i = i + 1;
|
||||||
@}
|
@}
|
||||||
@end example
|
@end example
|
||||||
@ -1360,10 +1360,10 @@ indicated with the unary @emph{star} operator @code{*}. Examples of such
|
|||||||
|
|
||||||
An example of an explicit-dereference operation performed on box values:
|
An example of an explicit-dereference operation performed on box values:
|
||||||
@example
|
@example
|
||||||
fn takes_boxed(@@int b) @{
|
fn takes_boxed(b: @@int) @{
|
||||||
@}
|
@}
|
||||||
|
|
||||||
fn takes_unboxed(int b) @{
|
fn takes_unboxed(b: int) @{
|
||||||
@}
|
@}
|
||||||
|
|
||||||
fn main() @{
|
fn main() @{
|
||||||
@ -1716,7 +1716,7 @@ mod foo @{
|
|||||||
helper(3, 4);
|
helper(3, 4);
|
||||||
@}
|
@}
|
||||||
|
|
||||||
fn helper(int x, int y) @{
|
fn helper(x: int, y: int) @{
|
||||||
@dots{}
|
@dots{}
|
||||||
@}
|
@}
|
||||||
@}
|
@}
|
||||||
@ -1754,7 +1754,7 @@ during compilation, returning the implicit @code{()} value.
|
|||||||
|
|
||||||
An example of a function:
|
An example of a function:
|
||||||
@example
|
@example
|
||||||
fn add(int x, int y) -> int @{
|
fn add(x: int, y: int) -> int @{
|
||||||
ret x + y;
|
ret x + y;
|
||||||
@}
|
@}
|
||||||
@end example
|
@end example
|
||||||
@ -1762,7 +1762,7 @@ fn add(int x, int y) -> int @{
|
|||||||
A special kind of function can be declared with a @code{!} character where the
|
A special kind of function can be declared with a @code{!} character where the
|
||||||
output slot type would normally be. For example:
|
output slot type would normally be. For example:
|
||||||
@example
|
@example
|
||||||
fn my_err(str s) -> ! @{
|
fn my_err(s: str) -> ! @{
|
||||||
log s;
|
log s;
|
||||||
fail;
|
fail;
|
||||||
@}
|
@}
|
||||||
@ -1781,7 +1781,7 @@ with a @code{ret}, @code{be}, or diverging expression. So, if @code{my_err}
|
|||||||
were declared without the @code{!} annotation, the following code would not
|
were declared without the @code{!} annotation, the following code would not
|
||||||
typecheck:
|
typecheck:
|
||||||
@example
|
@example
|
||||||
fn f(int i) -> int @{
|
fn f(i: int) -> int @{
|
||||||
if (i == 42) @{
|
if (i == 42) @{
|
||||||
ret 42;
|
ret 42;
|
||||||
@}
|
@}
|
||||||
@ -1849,7 +1849,7 @@ each} loop or as the argument in a @code{put each} expression.
|
|||||||
|
|
||||||
An example of an iterator:
|
An example of an iterator:
|
||||||
@example
|
@example
|
||||||
iter range(int lo, int hi) -> int @{
|
iter range(lo: int, hi: int) -> int @{
|
||||||
let i: int = lo;
|
let i: int = lo;
|
||||||
while (i < hi) @{
|
while (i < hi) @{
|
||||||
put i;
|
put i;
|
||||||
@ -1881,16 +1881,16 @@ constructor function when used in value context (such as a call).
|
|||||||
|
|
||||||
Example of an object item:
|
Example of an object item:
|
||||||
@example
|
@example
|
||||||
obj counter(int state) @{
|
obj counter(state: @@mutable int) @{
|
||||||
fn incr() @{
|
fn incr() @{
|
||||||
state += 1;
|
*state += 1;
|
||||||
@}
|
@}
|
||||||
fn get() -> int @{
|
fn get() -> int @{
|
||||||
ret state;
|
ret *state;
|
||||||
@}
|
@}
|
||||||
@}
|
@}
|
||||||
|
|
||||||
let c: counter = counter(1);
|
let c: counter = counter(@@mutable 1);
|
||||||
|
|
||||||
c.incr();
|
c.incr();
|
||||||
c.incr();
|
c.incr();
|
||||||
@ -2287,7 +2287,7 @@ slot. @xref{Ref.Item.Fn}.
|
|||||||
|
|
||||||
An example of a @code{fn} type:
|
An example of a @code{fn} type:
|
||||||
@example
|
@example
|
||||||
fn add(int x, int y) -> int @{
|
fn add(x: int, y: int) -> int @{
|
||||||
ret x + y;
|
ret x + y;
|
||||||
@}
|
@}
|
||||||
|
|
||||||
@ -2308,7 +2308,7 @@ constraints and an output slot. @xref{Ref.Item.Iter}.
|
|||||||
|
|
||||||
An example of an @code{iter} type:
|
An example of an @code{iter} type:
|
||||||
@example
|
@example
|
||||||
iter range(int x, int y) -> int @{
|
iter range(x: int, y: int) -> int @{
|
||||||
while (x < y) @{
|
while (x < y) @{
|
||||||
put x;
|
put x;
|
||||||
x += 1;
|
x += 1;
|
||||||
@ -2433,23 +2433,23 @@ a client function using both items via the object type:
|
|||||||
@example
|
@example
|
||||||
|
|
||||||
type taker =
|
type taker =
|
||||||
state obj @{
|
obj @{
|
||||||
fn take(int);
|
fn take(int);
|
||||||
@};
|
@};
|
||||||
|
|
||||||
obj adder(mutable int x) @{
|
obj adder(x: @@mutable int) @{
|
||||||
fn take(int y) @{
|
fn take(y: int) @{
|
||||||
x += y;
|
*x += y;
|
||||||
@}
|
@}
|
||||||
@}
|
@}
|
||||||
|
|
||||||
obj sender(chan[int] c) @{
|
obj sender(c: chan[int]) @{
|
||||||
fn take(int z) @{
|
fn take(z: int) @{
|
||||||
c <| z;
|
c <| z;
|
||||||
@}
|
@}
|
||||||
@}
|
@}
|
||||||
|
|
||||||
fn give_ints(taker t) @{
|
fn give_ints(t: taker) @{
|
||||||
t.take(1);
|
t.take(1);
|
||||||
t.take(2);
|
t.take(2);
|
||||||
t.take(3);
|
t.take(3);
|
||||||
@ -2457,7 +2457,7 @@ fn give_ints(taker t) @{
|
|||||||
|
|
||||||
let p: port[int] = port();
|
let p: port[int] = port();
|
||||||
|
|
||||||
let t1: taker = adder(0);
|
let t1: taker = adder(@@mutable 0);
|
||||||
let t2: taker = sender(chan(p));
|
let t2: taker = sender(chan(p));
|
||||||
|
|
||||||
give_ints(t1);
|
give_ints(t1);
|
||||||
@ -2939,7 +2939,7 @@ The result of a @code{spawn} expression is a @code{task} value.
|
|||||||
|
|
||||||
An example of a @code{spawn} expression:
|
An example of a @code{spawn} expression:
|
||||||
@example
|
@example
|
||||||
fn helper(chan[u8] out) @{
|
fn helper(out: chan[u8]) @{
|
||||||
// do some work.
|
// do some work.
|
||||||
out <| result;
|
out <| result;
|
||||||
@}
|
@}
|
||||||
@ -3033,7 +3033,7 @@ and residual arguments that was specified during the binding.
|
|||||||
|
|
||||||
An example of a @code{bind} expression:
|
An example of a @code{bind} expression:
|
||||||
@example
|
@example
|
||||||
fn add(int x, int y) -> int @{
|
fn add(x: int, y: int) -> int @{
|
||||||
ret x + y;
|
ret x + y;
|
||||||
@}
|
@}
|
||||||
type single_param_fn = fn(int) -> int;
|
type single_param_fn = fn(int) -> int;
|
||||||
@ -3070,7 +3070,7 @@ and transfers control to the caller frame.
|
|||||||
|
|
||||||
An example of a @code{ret} expression:
|
An example of a @code{ret} expression:
|
||||||
@example
|
@example
|
||||||
fn max(int a, int b) -> int @{
|
fn max(a: int, b: int) -> int @{
|
||||||
if (a > b) @{
|
if (a > b) @{
|
||||||
ret a;
|
ret a;
|
||||||
@}
|
@}
|
||||||
@ -3094,7 +3094,7 @@ last expression in a block.
|
|||||||
|
|
||||||
An example of a @code{be} expression:
|
An example of a @code{be} expression:
|
||||||
@example
|
@example
|
||||||
fn print_loop(int n) @{
|
fn print_loop(n: int) @{
|
||||||
if (n <= 0) @{
|
if (n <= 0) @{
|
||||||
ret;
|
ret;
|
||||||
@} else @{
|
@} else @{
|
||||||
@ -3182,7 +3182,7 @@ diagnostic buffer.
|
|||||||
|
|
||||||
An example of a @code{note} expression:
|
An example of a @code{note} expression:
|
||||||
@example
|
@example
|
||||||
fn read_file_lines(&str path) -> vec[str] @{
|
fn read_file_lines(path: &str) -> vec[str] @{
|
||||||
note path;
|
note path;
|
||||||
let r: vec[str];
|
let r: vec[str];
|
||||||
let f: file = open_read(path);
|
let f: file = open_read(path);
|
||||||
@ -3514,11 +3514,11 @@ and statically comparing implied states and their
|
|||||||
specifications. @xref{Ref.Typestate}.
|
specifications. @xref{Ref.Typestate}.
|
||||||
|
|
||||||
@example
|
@example
|
||||||
pred even(&int x) -> bool @{
|
pred even(x: &int) -> bool @{
|
||||||
ret x & 1 == 0;
|
ret x & 1 == 0;
|
||||||
@}
|
@}
|
||||||
|
|
||||||
fn print_even(int x) : even(x) @{
|
fn print_even(x: int) : even(x) @{
|
||||||
print(x);
|
print(x);
|
||||||
@}
|
@}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user