Thank you. Will take a look. I am looking for simple solutions. Patty is for
sure marvellous but 400 lines (or so) of code to emit few lines of code is
intimidating to me.
Not sure what will be easiest for you to use/understand..I thought the patty
approach pretty nice. That said, to answer your last question, the macros
module has a few interesting things along the lines of what you were asking
for: `parseExpr`, `parseStmt`, and `emit`. The first two can compile
So final version from my first post would now look like:
(I took variantp from andrea's patty and added my version of match)
template match*(t: typed, handlers: openArray): untyped =
var x = ord(t.kind)
handlers[x](t)
message.nim
import patty
import patty
variant Message:
Hit(hp: int)
Death(died: string)
proc writeMsg(m: Msg): string =
match m:
Hit(hp):
result = "HIT " & $hp
Death(died):
result = "DEATH " & died
Cannot get much
Thank you **andrea**.
I like your _variant_ but _match_ is for me similar in verbosity to my getText
and I think it could be compressed further.
My attempt:
import patty
variant Message:
hit(hp: int)
death(died: string)
proc hhit(m: Message) =
> but did not want to use inheritance and methods because it needs heap
> allocation
You are using strings so your objects are doing heap allocations anyway. You
should replace the strings with IDs of the target which got hit / died if you
don't want any heap allocation.
You can of course do
I have created two macros exactly for this purpose!
[https://github.com/andreaferretti/patty](https://github.com/andreaferretti/patty)
The first one, called `variant` or `variantp` (for public), generate the types
and the three constructor functions.
The second one, called `match`, can be used
Hi
I needed some sort of polymorphism, but did not want to use inheritance and
methods because it needs heap allocation and I have quite small objects which
are created and destroyed frequently.
Finally I came out with setup which I was comfortable with, and here is example:
message.nim