Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import-from "none.libc" printf
import-from "liminal.funclist" FunctionList
; three observing static functions
static func1 (x) :
void <- (int)
printf "func1 %i\n" x
static func2 (x) :
void <- (int)
printf "func2 %i\n" x
; an implicit context for the third observer
struct Context
value : int
method on-call (self x) :
void <- (&this-struct int)
printf "func3 %i+%i = %i\n" self.value x (self.value + x)
const observer3 : Context
observer3.value = 42
; define new function list
var caller
FunctionList ;
; add observers
caller.append func1
caller.append func2
; third observer is a bound method
caller.append observer3.on-call
; function is cached
assert (caller.callback and (caller.callback is caller.callback))
; broadcast function
static mainfunc (x) :
void <- (int)
caller.callback x
; look at assembly to verify that all calls are inlined
mainfunc --> (disas)
; test call
mainfunc 303
struct ObservedValue
value : int
on-change :=
FunctionList ;
method set-value (self x) :
void <- (&this-struct int)
self.value = x
self.on-change x
; note that observation is not per instance, but for all instances
static (self x) :
void <- (&ObservedValue int)
printf "func1 %i %i\n" self.value x
; static macros can also be attached
static-macro (self x)
printf "func2 %i %i\n" self.value x
const myvalue : ObservedValue
myvalue.set-value 500
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.