Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic continuations #93

Open
zevv opened this issue May 13, 2021 · 8 comments
Open

Generic continuations #93

zevv opened this issue May 13, 2021 · 8 comments
Labels
enhancement New feature or request

Comments

@zevv
Copy link
Collaborator

zevv commented May 13, 2021

What would be needed to get generic continuations like in stash/iteratorT.nim to work?

@alaviss
Copy link
Contributor

alaviss commented May 14, 2021

For the most part we need to clone T to the env and the continuations.

@zevv zevv closed this as completed May 14, 2021
@disruptek
Copy link
Contributor

@zevv isn't going to wait around for generics, I guess.

@zevv
Copy link
Collaborator Author

zevv commented May 14, 2021

After a bit of a think I realized that this has probably not the highest prio, and/or we could (should) even do without them; thus the premature closing

@zevv zevv reopened this May 22, 2021
@zevv
Copy link
Collaborator Author

zevv commented May 22, 2021

More thinking, and I can't make up my mind. I think we do need generics after all :)

@zevv
Copy link
Collaborator Author

zevv commented May 22, 2021

This already works:

  C1[T] = ref object of RootObj   
    fn: proc(c: C1[T]): C1[T]   
    val: T   
 
proc magic1[T](c: C1[T], v: T) {.cpsMagic.} =       # <-- needs extra arg of type T
  echo "magic ", type(T)   
  c.val = v   
 
proc foo1(val: int) {.cps:C1[int].}=                # <-- can't use foo[T](val: T): {.cps:C1[T].} 
  echo "foo[", type(int), "]"   
  magic1(val)   
 
var c1 = foo1(42)   
c1 = c1.fn(c1)   
  • foo1() is not fully generic, it's still working on C1[int] only
  • Nim can only understand how to find magic1[T]() because of the v: T argument. When this is left off, the "fake" rewrite of the magic function looks like this: proc magic2[T]() {.cpsVoodooCall.} = so there is no T left for Nim to use.

This is the fully generic version that exposes the above two problems, and does not compile:

type   
  C2[T] = ref object of RootObj   
    fn: proc(c: C2[T]): C2[T]   
    val: T   
 
proc magic2[T](c: C2[T]) {.cpsMagic.} =   
  echo "magic ", type(T)   
  c.val = 42   
 
proc foo[T]() {.cps:C2[T].}=   
  echo "foo[", type(T), "]"   
  magic2[T]()   
 
var c2 = foo2[int]()   
c2 = c2.fn(c2)   

@disruptek disruptek added the enhancement New feature or request label May 23, 2021
@alaviss
Copy link
Contributor

alaviss commented Jun 14, 2021

As discussed on IRC, the verdict on this is that we have to be able to work on the instantiated tree as the generics tree is basically untyped.

@blackmius
Copy link

blackmius commented Dec 29, 2023

I dont know if it is related or i need to create another issue. I found that generic proc arguments cannot be lifted in continuation. for example

proc a(n: int | float) {.cps: Continuation.} =
  echo n

and this not compiling

of course i can make compiler work myself

type C = Continuation
template aImpl() {.dirty.} =
  echo n
proc a(n: int) {.cps: C} =
  aImpl()
proc a(n: float) {.cps: C} =
  aImpl()

but is it the most convenient way?

@alaviss
Copy link
Contributor

alaviss commented Dec 30, 2023

It's the same issue. As of today the problem remains the same, we don't have a good way to only work on an instantiated Nim tree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants