Skip to content

Instantly share code, notes, and snippets.

@yuanweixin
Created February 8, 2023 11:03
Show Gist options
  • Save yuanweixin/ce041c0def7d0afb7dbff90c9f2a7533 to your computer and use it in GitHub Desktop.
Save yuanweixin/ce041c0def7d0afb7dbff90c9f2a7533 to your computer and use it in GitHub Desktop.
factory pattern with nim concepts
type Frame* = concept x, type T
T.newFrame is T
type Arch = enum
x86
mips
proc translateExp[T : Frame]() =
discard T.newFrame()
type
X86Frame = object
i: int
MipsFrame = object
i:int
proc newFrame(x:typedesc[X86Frame]) : X86Frame =
return X86Frame(i:42)
proc newFrame(x:typedesc[MipsFrame]) : MipsFrame =
return MipsFrame(i:42)
let arch = mips
case arch
of x86:
translateExp[X86Frame]()
of mips:
translateExp[MipsFrame]()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment