@@ -127,9 +127,9 @@ the large'', that is, of creating and maintaining @emph{boundaries} -- both
127
127
abstract and operational -- that preserve large-system @emph {integrity },
128
128
@emph {availability } and @emph {concurrency }.
129
129
130
- It supports a mixture of imperative procedural, concurrent actor, object
131
- oriented and pure functional styles. Rust also supports generic programming
132
- and metaprogramming, in both static and dynamic styles.
130
+ It supports a mixture of imperative procedural, concurrent actor,
131
+ object- oriented and pure functional styles. Rust also supports generic
132
+ programming and metaprogramming, in both static and dynamic styles.
133
133
134
134
@menu
135
135
* Goals :: Intentions, motivations.
@@ -1894,52 +1894,28 @@ let c: counter = counter(1);
1894
1894
1895
1895
c.incr();
1896
1896
c.incr();
1897
- assert ( c.get() == 3) ;
1897
+ assert c.get() == 3;
1898
1898
@end example
1899
1899
1900
- There is no @emph {this } or @emph {self } available inside an object's
1901
- methods, either implicitly or explicitly, so you can't directly call
1902
- other methods. For example:
1900
+ Inside an object's methods, you can make @emph {self-calls } using the
1901
+ @code {self } keyword.
1903
1902
@example
1904
1903
obj my_obj() @{
1905
1904
fn get() -> int @{
1906
1905
ret 3;
1907
1906
@}
1908
- fn foo() @{
1909
- let c = get(); // Fails
1907
+ fn foo() -> int @{
1908
+ let c = self.get();
1909
+ ret c + 2;
1910
1910
@}
1911
1911
@}
1912
- @end example
1913
1912
1914
- The current replacement is to write a factory function for your type,
1915
- which provides any private helper functions:
1916
- @example
1917
- type my_obj =
1918
- obj @{
1919
- fn get() -> int;
1920
- fn foo();
1921
- @} ;
1922
-
1923
- fn mk_my_obj() -> my_obj @{
1924
- fn get_helper() -> int @{
1925
- ret 3;
1926
- @}
1927
-
1928
- obj impl() @{
1929
- fn get() -> int @{
1930
- ret get_helper();
1931
- @}
1932
- fn foo() @{
1933
- let c = get_helper(); // Works
1934
- @}
1935
- @}
1936
-
1937
- ret impl();
1938
- @}
1913
+ let o = my_obj();
1914
+ assert o.foo() == 5;
1939
1915
@end example
1940
1916
1941
- This factory function also allows you to bind the object's state
1942
- variables to initial values .
1917
+ Rust objects are extendable with additional methods and fields using
1918
+ @emph { anonymous object } expressions. @xref { Ref.Expr.AnonObj } .
1943
1919
1944
1920
@node Ref.Item.Type
1945
1921
@subsection Ref.Item.Type
@@ -2911,6 +2887,7 @@ effects of the expression's evaluation.
2911
2887
* Ref.Expr.Claim :: Expression for static (unsafe) or dynamic assertion of typestate.
2912
2888
* Ref.Expr.Assert :: Expression for halting the program if a boolean condition fails to hold.
2913
2889
* Ref.Expr.IfCheck :: Expression for dynamic testing of typestate.
2890
+ * Ref.Expr.AnonObj :: Expression for extending objects with additional methods.
2914
2891
@end menu
2915
2892
2916
2893
@@ -3632,6 +3609,13 @@ the condition may be any boolean-typed expression, and the compiler makes no
3632
3609
use of the knowledge that the condition holds if the program continues to
3633
3610
execute after the @code {assert }.
3634
3611
3612
+ @node Ref.Expr.AnonObj
3613
+ @subsection Ref.Expr.AnonObj
3614
+ @c * Ref.Expr.AnonObj:: Expression that extends an object with additional methods.
3615
+ @cindex Anonymous objects
3616
+
3617
+ An @emph {anonymous object } expression extends an existing object with methods.
3618
+
3635
3619
@page
3636
3620
@node Ref.Run
3637
3621
@section Ref.Run
0 commit comments