Skip to content

Commit bd56de7

Browse files
jyasskingraydon
authored andcommitted
Explain that rust methods can't call other methods on the same object, either
implicitly or explicitly.
1 parent c3c425e commit bd56de7

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

doc/rust.texi

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,50 @@ c.incr();
18111811
check (c.get() == 3);
18121812
@end example
18131813

1814+
There is no @emph{this} or @emph{self} available inside an object's
1815+
methods, either implicitly or explicitly, so you can't directly call
1816+
other methods. For example:
1817+
@example
1818+
obj my_obj() @{
1819+
fn get() -> int @{
1820+
ret 3;
1821+
@}
1822+
fn foo() @{
1823+
auto c = get(); // Fails
1824+
@}
1825+
@}
1826+
@end example
1827+
1828+
The current replacement is to write a factory function for your type,
1829+
which provides any private helper functions:
1830+
@example
1831+
type my_obj =
1832+
obj @{
1833+
fn get() -> int;
1834+
fn foo();
1835+
@};
1836+
1837+
fn mk_my_obj() -> my_obj @{
1838+
fn get_helper() -> int @{
1839+
ret 3;
1840+
@}
1841+
1842+
obj impl() @{
1843+
fn get() -> int @{
1844+
ret get_helper();
1845+
@}
1846+
fn foo() @{
1847+
auto c = get_helper(); // Works
1848+
@}
1849+
@}
1850+
1851+
ret impl();
1852+
@}
1853+
@end example
1854+
1855+
This factory function also allows you to bind the object's state
1856+
variables to initial values.
1857+
18141858
@node Ref.Item.Type
18151859
@subsection Ref.Item.Type
18161860
@c * Ref.Item.Type:: Items defining the types of values and slots.

0 commit comments

Comments
 (0)