arturo
arturo copied to clipboard
Create new `:event` type and related handlers/emitters as built-in functions?
See also:
https://discord.com/channels/765519132186640445/829324913097048065/1084769083771330632
About this new type, how does should it work?
Well, the truth is this is something that remains to be studied.
A few thoughts...
- we could have 2 different functions, one for handling events (
on
) and one for emitting/triggering events (emit
). - there could be system-defined events (e.g. Nim clearly supports adding a hook for Ctrl+C events -> https://nim-lang.github.io/Nim/system.html#setControlCHook%2Cproc%29), which we could properly handle if necessary
- the user may also create different event on his own
- all of this would have to comply to a new
:event
type - each event could also carry some data (perhaps any valid Arturo value encapsulated)
A rough example of what I'm talking about:
on 'ctrlC [e][
print ["Received control-c event:" e]
]
or...
setNum: function [x,val][
previousVal: var'x
let x val
emit.with:#[
previous: previousVal
current: var'x
] 'numberChanged
]
on 'numberChanged [e][
print "Number changed!"
print ["previous value:" e\data\previousVal "current value:" e\data\currentVal]
]
a: 2
setNum 'a 3
print a ; a has become 3, and the event has been triggered in the background
That's a very rough sketch, but this is pretty much what I've had in mind 😉
The whole idea is very, very similar to that of functions; and that's a reason why I haven't implemented it yet. Now, that being said, the Ctrl-C idea for example is something that could be used with events (but obviously not with functions, as they are in Arturo), given that the "emitter" is the system itself.
Thanks for the explanation, I liked this idea!
Thanks for the explanation, I liked this idea!
Let's see... This needs some careful thought before!
For now, I would prefer it if we focused on existing issues & testing rather than adding something totally new, since - as it happens - with new features come new bugs. But it's definitely in my to-do list!
For now, I would prefer it if we focused on existing issues & testing rather than adding something totally new,
I totally agree with it! 😅
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Closing issue as stale.