Skip to content

Instantly share code, notes, and snippets.

@tersec
Created April 7, 2021 13:11
Show Gist options
  • Save tersec/b844ba0bf67a828b2463a2ef81e49740 to your computer and use it in GitHub Desktop.
Save tersec/b844ba0bf67a828b2463a2ef81e49740 to your computer and use it in GitHub Desktop.
type
A = enum
b
c
K = object
y*: string
z*: int
J = object
# K and J aren't nominally-related, but structurally-related types
y*: string
z*: int
D* = object
case e: A
of b: f*: K
of c: g*: J
template h(i: D, j: untyped): untyped =
if i.e == b:
i.f.j
else:
i.g.j
# Deliberately runtime, in principle
let
m = D(e: b, f: K(y: "foo", z: 3))
n = D(e: c, g: J(y: "bar", z: 7))
echo h(m, y)
echo h(m, z)
echo h(n, y)
echo h(n, z)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment