Created
February 8, 2023 11:03
-
-
Save yuanweixin/ce041c0def7d0afb7dbff90c9f2a7533 to your computer and use it in GitHub Desktop.
factory pattern with nim concepts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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