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

initFromCL doesn't work with ref object #118

Closed
timotheecour opened this issue Sep 13, 2019 · 6 comments
Closed

initFromCL doesn't work with ref object #118

timotheecour opened this issue Sep 13, 2019 · 6 comments

Comments

@timotheecour
Copy link
Contributor

#[
Error: default value is not a tuple or object
    else: error "default value is not a tuple or object"
]#

type Foo = ref object # bug
# type Foo = object # ok
  a1, a2: string
let dfl* = Foo(a1: "junk")
proc main(a: Foo) = discard

when isMainModule:
  import cligen
  var app = initFromCL(dfl)
  app.main()
@c-blake
Copy link
Owner

c-blake commented Sep 13, 2019

Ok. Any thoughts on how to make it work? Does getTypeImpl work on such in other contexts?

@timotheecour
Copy link
Contributor Author

timotheecour commented Sep 13, 2019

untried, but maybe something like:

proc process[T](dfl: T) =
  when T is ref | ptr:
    doAssert dfl != nil # could handle this too maybe
    process(dfl[])
  elif T is object:
    sameAsBefore()

@c-blake
Copy link
Owner

c-blake commented Sep 13, 2019

dfl[].getTypeImpl does not work. What we need is to peel the ref part of the type off. We may then need either autoderef or a bunch of [] elsewhere in generated code, but the first step is looping over the right .children.

@c-blake
Copy link
Owner

c-blake commented Sep 13, 2019

E.g., it looks like we could do a branch after of ntyObject

  of ntyRef: ti = ti[0].getTypeImpl[2]

That actually makes your example compile and even generates the correct help message! Could probably do similar for ptr and ptr|ref to tuple types. Running fails in the generated setters, though. We probably just need to set a flag in that branch and elsewhere do the [] conditionally upon that flag. PR welcome if you want to work on it. I won't have time to do more until at least this weekend.

@c-blake
Copy link
Owner

c-blake commented Sep 21, 2019

I could be more careful about checking that the T in ptr|ref T is an object and this could (maybe?) be generalized to tuples, but the basic usage you were asking for should work. PRs welcome if you want to dot those i's and cross those t's.

@timotheecour
Copy link
Contributor Author

@c-blake wonderful, thanks!

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

No branches or pull requests

2 participants