Skip to content

Instantly share code, notes, and snippets.

@paniq
Created September 27, 2015 07:31
Show Gist options
  • Save paniq/2283738e0eaf32fcea85 to your computer and use it in GitHub Desktop.
Save paniq/2283738e0eaf32fcea85 to your computer and use it in GitHub Desktop.
test_funclist.n
none
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
ObservedValue.on-change.append
static (self x) :
void <- (&ObservedValue int)
printf "func1 %i %i\n" self.value x
; static macros can also be attached
ObservedValue.on-change.append
static-macro (self x)
static-quote
printf "func2 %i %i\n" self.value x
const myvalue : ObservedValue
myvalue.set-value 500
null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment