Actors & Classes
What's the difference?
In Pony, logic inside a class is run synchronously, whereas actors are run with asychronous communication.
Actors replace threads for concurrency.
Constructors
Constructors are defined by the new keyword:
new my_constructor([args]) =>
...An actor or class can have multiple constructors with different names.
Default constructor
The default constructor for an agent or class is create:
actor Main
new create([args]) =>
...Instantiating
You instantiate a class or actor like so:
Fields
Fields are defined as such:
The underscore before the field's name denotes it as being private. In this case we don't assign a value to the field, and so the code won't compile unless we assign it a value in the constructor.
Return Types
When defining behaviours or functions, specifying the return type is important. If no return type is specified, then the compiler will assume that the function returns a None type.
Classes
Functions
In classes, functions are defined like so:
apply function
apply functionThe apply function is meant to implement some common functionality of a class, and can be called directly. For example:
As you can see you don't need to call my_class.apply("String one", "String two"), and you can just omit .apply.
Actors
Behaviours
In actors, rather than defining functions, you define behaviours using the be keyword:
Last updated
Was this helpful?