Skip to content

Instantly share code, notes, and snippets.

@flaviut
Created May 25, 2014 21:22
Show Gist options
  • Save flaviut/95b7b7cfcc318135e45f to your computer and use it in GitHub Desktop.
Save flaviut/95b7b7cfcc318135e45f to your computer and use it in GitHub Desktop.
import tables
type
ContextValue[T] = object
value: ref T
MyString = object of TObject
value: string
ExtendedObj = object of TObject
name: string
# Create context value
var x_ctxVal: ContextValue[ExtendedObj]
new(x_ctxVal.value)
x_ctxVal.value.name = "foo"
var y_ctxVal: ContextValue[MyString]
new(y_ctxVal.value)
y_ctxVal.value.value = "foo"
# Create context
var Context = initTable[string, ContextValue[any]]()
# Adds value to context
Context["x"] = cast[ContextValue[any]](x_ctxVal)
Context["y"] = cast[ContextValue[any]](y_ctxVal)
# Prints context value name
echo(cast[ContextValue[ExtendedObj]](Context["x"]).value.name)
echo(cast[ContextValue[MyString]](Context["y"]).value.value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment