Skip to content

Commit e241250

Browse files
authored
Fix documentation about importing methods (#1262)
1 parent fd32325 commit e241250

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

doc/imports.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ Practically, here's what you need to know:
3636

3737
## Public
3838

39-
If you want a function, [class](classes.md), [enum](enums.md) or global variable to be importable,
39+
If you want a function, [method](classes.md#methods), [class](classes.md), [enum](enums.md),
40+
[constant](keywords.md#const) or [global variable](keywords.md#global) to be importable,
4041
make it public with the `@public` decorator.
4142
Anything not decorated with `@public` can only be used in the same file.
4243

@@ -52,13 +53,14 @@ def visible_in_files_that_import_this_file() -> None:
5253

5354
class OnlyForThisFile:
5455
field: int
55-
def method(self) -> None:
56+
def private_method(self) -> None:
5657
...
5758

5859
@public
5960
class VisibleInFilesThatImportThisFile:
6061
field: int
61-
def method(self) -> None:
62+
@public
63+
def public_method(self) -> None:
6264
...
6365

6466
global kinda_ok_variable: int
@@ -67,14 +69,19 @@ global kinda_ok_variable: int
6769
global many_people_consider_this_bad: int
6870
```
6971

70-
Class fields and methods are always public.
71-
This means that if you have an instance of a class,
72-
you can simply call any method defined in the class.
73-
If you don't like this, please create an issue on GitHub to discuss it.
72+
Class fields are always public: if you have an instance of a class, you can access the data stored inside it.
73+
74+
Importing methods is just like importing functions:
75+
to call a method defined in a different file,
76+
the method must be `@public` and you need to import the file that defines the method.
77+
You may find this surprising, because in most languages,
78+
public methods are always accessible regardless of imports,
79+
but Jou methods are basically functions with slightly different syntax.
7480

7581
Many people dislike global variables, especially when they are accessed in multiple files.
7682
That said, Jou lets you decorate a global variable as `@public` and import it into another file,
7783
because sometimes that is the best solution.
84+
For example, [the `jou_assert_fail_handler` variable in stdlib/assert.jou](assert.md#the-jou_assert_fail_handler-global-variable) works this way.
7885

7986

8087
## Conflicting Names

0 commit comments

Comments
 (0)