From bd56de745b64d6d3eb8b64f9716ca08811a6fd72 Mon Sep 17 00:00:00 2001 From: Jeffrey Yasskin Date: Sun, 18 Jul 2010 03:15:14 +0800 Subject: [PATCH] Explain that rust methods can't call other methods on the same object, either implicitly or explicitly. --- doc/rust.texi | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/doc/rust.texi b/doc/rust.texi index 0b6f0cae5ab..1f780d6f800 100644 --- a/doc/rust.texi +++ b/doc/rust.texi @@ -1811,6 +1811,50 @@ c.incr(); check (c.get() == 3); @end example +There is no @emph{this} or @emph{self} available inside an object's +methods, either implicitly or explicitly, so you can't directly call +other methods. For example: +@example +obj my_obj() @{ + fn get() -> int @{ + ret 3; + @} + fn foo() @{ + auto c = get(); // Fails + @} +@} +@end example + +The current replacement is to write a factory function for your type, +which provides any private helper functions: +@example +type my_obj = + obj @{ + fn get() -> int; + fn foo(); + @}; + +fn mk_my_obj() -> my_obj @{ + fn get_helper() -> int @{ + ret 3; + @} + + obj impl() @{ + fn get() -> int @{ + ret get_helper(); + @} + fn foo() @{ + auto c = get_helper(); // Works + @} + @} + + ret impl(); +@} +@end example + +This factory function also allows you to bind the object's state +variables to initial values. + @node Ref.Item.Type @subsection Ref.Item.Type @c * Ref.Item.Type:: Items defining the types of values and slots.