Skip to content

Commit 33b1733

Browse files
authored
Explain actor instantiation
Previously, some folks got confused by finer details of interacting with actors. The earlier content referred to "same as classes" and that wasn't clear to everyone. Closes #469
1 parent 874c45e commit 33b1733

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

code-samples/actors-behaviors.pony

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
actor Main
2+
new create(env: Env) =>
3+
let aardvark = Aardvark("Arnie")
4+
aardvark.eat(99)
5+
16
actor Aardvark
27
let name: String
38
var _hunger_level: U64 = 0

docs/types/actors.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
# Actors
22

3-
An __actor__ is similar to a __class__, but with one critical difference: an actor can have __behaviours__.
3+
In Pony, __classes__ and __actors__ share many similarities and much of what you've already learned about the former directly transfers to the latter.
4+
5+
## Declaration
6+
7+
Let's start by looking at how syntactically similar an actor declaration is to a class declaration. We can take one of our earlier "class Wombat" examples and turn it into an "actor Aardvark" simply by changing the `class` keyword to `actor` and renaming to "Aardvark".
8+
9+
```pony
10+
--8<-- "actors-behaviors.pony:6:12"
11+
```
12+
13+
## Instantiation
14+
15+
When it comes time to instantiate an Aardvark, the syntactic similarities continue: We instantiate actors the same way we instantiate objects. Unlike some other actor-oriented languages and frameworks, there's no special syntax or method to "spawn" an actor in Pony.
16+
17+
```pony
18+
--8<-- "actors-behaviors.pony:3:3"
19+
```
20+
21+
Despite these similarities, it's important to note that there are substantial differences between actors and objects when it comes to how the rest of your Pony code will interact with them. After it's instantiated, all interaction with an actor must be through its **behaviours**. Since our Aardvark doesn't yet have any, we'll need to add some if we want it to be useful.
422

523
## Behaviours
624

@@ -15,10 +33,16 @@ Like a function, a behaviour can have parameters. Unlike a function, it doesn't
1533
__So what does a behaviour return?__ Behaviours always return `None`, like a function without explicit result type, because they can't return something they calculate (since they haven't run yet).
1634

1735
```pony
18-
--8<-- "actors-behaviors.pony"
36+
--8<-- "actors-behaviors.pony:5"
37+
```
38+
39+
Here we have an `Aardvark` that can eat asynchronously. Clever Aardvark. To make it eat, we simply call the "eat" behavior on that instance.
40+
41+
```pony
42+
--8<-- "actors-behaviors.pony:4:5"
1943
```
2044

21-
Here we have an `Aardvark` that can eat asynchronously. Clever Aardvark.
45+
This looks just like a synchronous function call but we're actually **passing a message** to `aardvark` requesting that the behavior be executed asynchronously.
2246

2347
## Message Passing
2448

0 commit comments

Comments
 (0)