Skip to content

Instantly share code, notes, and snippets.

@flaviut
Created May 25, 2014 21:09
Show Gist options
  • Save flaviut/dfa9dbf772368a6fc9cd to your computer and use it in GitHub Desktop.
Save flaviut/dfa9dbf772368a6fc9cd 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[TObject]]()
# Adds value to context
Context["x"] = cast[ContextValue[TObject]](x_ctxVal)
Context["y"] = cast[ContextValue[TObject]](y_ctxVal) # Error: type mismatch: got (TTable[string, ContextValue[ExtendedObj]], string, ContextValue[string])
# 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