Skip to content

Instantly share code, notes, and snippets.

@porky11
Last active November 13, 2018 16:03
Show Gist options
  • Save porky11/125a3419fd99cb632b7bcf0573ec2194 to your computer and use it in GitHub Desktop.
Save porky11/125a3419fd99cb632b7bcf0573ec2194 to your computer and use it in GitHub Desktop.
Dynamic bindings in scope
let model model-normal =
static mat4
static mat3
typefn& Node 'render (self)
dynamic-let model model-normal =
'transform model self.transform
'transform model-normal self.transform-normal
if ('is-leaf self)
render-mesh self.mesh model model-normal
else
for child in self.children
'render child
let model model-normal =
static mat4
static mat3
typefn& Node 'render (self)
let old-model old-model-normal =
deref model
deref model-normal
model =
'transform model self.transform
model-normal =
'transform model-normal self.transform-normal
if ('is-leaf self)
render-mesh self.mesh model model-normal
else
for child in self.children
'render child
model = old-model
model-normal = old-model-normal
typefn& Node 'render (self model model-normal)
let model model-normal =
'transform model self.transform
'transform model-normal self.transform-normal
if ('is-leaf self)
render-mesh self.mesh model model-normal
else
for child in self.children
'render child model model-normal
@porky11
Copy link
Author

porky11 commented Nov 13, 2018

dynamic:
this version uses the new scope-macro dynamic-let, which temporary changes the globals for the current scope

explicit:
this version does exactly the same, but without the use of the new scope macro, but instead typing everything explicitly

pass-around:
this version does the same, but passes everything around instead of using global variables
this doesn't need global variables, but you have to pass everything around
it may get inconvenient when having too many global variables
you may pack these variables into a struct or pass them around as variadic arguments, which will make it more convenient to pass around, but it's still necessary to pass it around at all
the problem when using a struct is, that you either have to revert the state anyway or you have a copy, where most of the variables are just the same
it's also more difficult to write generic code, when the API is not intended to use some global state, but you need global state in your use case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment