Re: How to be more concise in code [NEWBIE]

2017-02-16 Thread mmierzwa
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.

Re: How to be more concise in code [NEWBIE]

2017-02-16 Thread cblake
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

Re: How to be more concise in code [NEWBIE]

2017-02-16 Thread mmierzwa
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

Re: How to be more concise in code [NEWBIE]

2017-02-16 Thread andrea
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

Re: How to be more concise in code [NEWBIE]

2017-02-16 Thread mmierzwa
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) =

Re: How to be more concise in code [NEWBIE]

2017-02-16 Thread flyx
> 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

Re: How to be more concise in code [NEWBIE]

2017-02-16 Thread andrea
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

How to be more concise in code [NEWBIE]

2017-02-16 Thread mmierzwa
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