nim-works / cps

Continuation-Passing Style for Nim 🔗

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The code in the doc doesn't work

geohuz opened this issue · comments

I've tried to folllow the cps doc with following code:

type 
  Cont = ref object
    fn: proc(c: Cont): Cont
    n: int
    i: int

proc count(n: int): Cont =
  return Cont(fn: count, n: n)


proc whileLoop(c: Cont): Cont =
  if c.i<c.n:
    c.fn = afterCall
    return jield(c)
  else:
    c.fn = done
    return c

proc count(c: Cont): Cont =
  echo "start"
  c.i = 0
  c.fn = whileLoop
  return c

proc afterCall(c: Cont): Cont =
  inc c.i
  c.fn = whileLoop
  return c

proc done(c: Cont): Cont =
  echo "end"
  return nil


var c = count(2)
while c.fn != nil:
  c = c.fn(c)

and I got the error: Error: type mismatch: got <proc (n: int): Cont> but expected 'proc (c: Cont): Cont{.closure.}', also I think there is a circle dependency between these procs, is there a full working code for the doc?

There isn't a single reference, not even an import, of the CPS library in your code. So I'm guessing the docs are proving difficult to discover perhaps.

Linked off the readme there is a tutorial that has you write a CPS program and explains very line as you go. I suggest staying with that: https://github.com/disruptek/cps/blob/master/tutorial/README.md

Out of curiosity, were you able to find the tutorial that I linked above before opening this issue?

I found the CPS explaination by following this link: https://github.com/disruptek/cps/tree/master/docs
Thanks for the tutorial! It must be the right place I should take a look ;-)

Hi @geohu,

We clearly need to spend some more time on organizing and cleaning up the different docs we have, by @seam is right: if you want to get started with CPS, the tutorial is the place to go. The doc/README.md is ment to provide a more general overview of CPS concepts, but is not ment to serve a s a document you can follow step-by-step.

I'm not sure if we can/want to fix all code in the doc/README.md, but if you find anything that is not working for you in the tutorial, be sure to shout out and we'll fix that!