Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain more pointer stuff in tutorial #501

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,7 @@ def main() -> int:
return 0
```

Instead of pointers, you could also use an `int[2]` array to return the two values.
However, **this doesn't mean that you don't need to understand pointers**,
as they have many other uses in Jou.
Instead of pointers, you could also use an `int[2]` array to return the two values:

```python
import "stdlib/io.jou"
Expand All @@ -331,6 +329,25 @@ def main() -> int:
return 0
```

However, **this doesn't mean that you don't need to understand pointers**,
as they have many other uses in Jou.
Pointers are used for strings, arrays that are not fixed-size,
instances of most classes, and so on.

Basically, you need pointers whenever you want to use a large object in multiple places
without making several copies of it.
Instead, you just make one object and point to it from many places.
This is probably what you expect if you have mostly used high-level languages,
like Python or JavaScript.
In fact, in Python, **all** objects are passed around as pointers.

You usually don't need pointers for small objects.
For example, if you want to make a function takes two `int`s and prints them,
just make a function that takes two `int`s.
On the other hand, if your function takes an array of 100000 `int`s,
you should use a pointer.
Passing around hundreds or thousands of bytes without pointers is usually a bad idea.


## Undefined Behavior (UB)

Expand Down
Loading