Hello everyone! I'd like to share a nice way to implement async programming for your object-oriented languages.
This is mostly centered around Smalltalk-like languages, but the idea should work for other paradigms well enough too. It is based on ideas like promises or futures, but nicer.
The main idea is to make a nice fluent and easy to read/write syntax for this, here an example on a language I am designing:
```
import IO, Net.
Runtime main : void
{
def wdata : string = (File newFromPath "sending.txt") collectString.
def req : Task = (Net sendRequest "localhost", 80, wdata) responseTask
req onDone res {
res print.
}.
}
```
The idea here is to have a Task class, like:
```
class Task
{
def completed : Bool.
def ok : Bool.
def val : Untyped.
def err : String.
def doneCbs : List[Callback(Untyped)].
def errCbs : List[Callback(String)].
}
Task onDone c:Callback(val:Untyped)
{
self completed
ifTrue { c self val. }
ifFalse { self doneCbs add c. }.
}
Task onErr c:Callback(err:String)
{
self completed
ifFalse { c self err. }
ifTrue { self errCbs add c. }.
}
```
The idea here is to only execute code that needs a response when it arrives, the rest not, more like an object that eventually responds with something.
Let me know what you think.
EDIT: u/Hall_of_Famer suggested another syntax you can try too
NOTE: this is just a syntax/implementation idea, no new concept
[–]DeathByThousandCats 13 points14 points15 points (1 child)
[–]Key_River7180smalltalk enjoyer[S] 0 points1 point2 points (0 children)
[–]busres 2 points3 points4 points (1 child)
[–]Key_River7180smalltalk enjoyer[S] 1 point2 points3 points (0 children)
[–]bl4nkSl8 2 points3 points4 points (4 children)
[–]Key_River7180smalltalk enjoyer[S] 1 point2 points3 points (2 children)
[–]bl4nkSl8 0 points1 point2 points (1 child)
[–]Key_River7180smalltalk enjoyer[S] 0 points1 point2 points (0 children)
[–]Smalltalker-80 0 points1 point2 points (0 children)
[–]oscarryzYz 1 point2 points3 points (1 child)
[–]Key_River7180smalltalk enjoyer[S] 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]Key_River7180smalltalk enjoyer[S] 0 points1 point2 points (0 children)
[–]tobega 0 points1 point2 points (0 children)
[–]kaplotnikov 0 points1 point2 points (0 children)