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

Calling newColumnView() does not compile. #206

Open
StefanSalewski opened this issue Aug 31, 2022 · 2 comments
Open

Calling newColumnView() does not compile. #206

StefanSalewski opened this issue Aug 31, 2022 · 2 comments

Comments

@StefanSalewski
Copy link
Owner

#[
proc newColumnView*(model: SelectionModel | MultiSelection | NoSelection | SingleSelection = nil): ColumnView =
  let gobj = gtk_column_view_new(if model.isNil: nil else: cast[ptr SelectionModel00](g_object_ref(model.impl)))
  let qdata = g_object_get_qdata(gobj, Quark)
  if qdata != nil:
    result = cast[type(result)](qdata)
    assert(result.impl == gobj)
  else:
    fnew(result, gtk4.finalizeGObject)
    result.impl = gobj
    GC_ref(result)
    discard g_object_ref_sink(result.impl)
    g_object_add_toggle_ref(result.impl, toggleNotify, addr(result[]))
    g_object_unref(result.impl)
    assert(g_object_get_qdata(result.impl, Quark) == nil)
    g_object_set_qdata(result.impl, Quark, addr(result[]))
]#

type
  A = ref object
    impl: pointer
    i: int

  B = ref object
    impl: pointer
    j: int

#proc t(x: A = nil) = # works
proc t(x: A | B = nil) =
  echo(if x == nil: "nil" else: $(cast[int](x.impl)))

proc main =
  var h: A
  t() # Error: cannot instantiate: 'T'
  t(h) # works
  t(x = nil) # Error: undeclared field: 'impl'

main()
@beef331
Copy link

beef331 commented Aug 31, 2022

nil without a type and no way to infer the type is typed to typeof(nil), so simply doing

type
  A = ref object
    impl: pointer
    i: int

  B = ref object
    impl: pointer
    j: int

#proc t(x: A = nil) = # works
proc t(x: A | B = A(nil)) =
  echo(if x == nil: "nil" else: $(cast[int](x.impl)))

proc main =
  var h: A
  t() # Error: cannot instantiate: 'T'
  t(h) # works
  t(x = B(nil)) # Error: undeclared field: 'impl'

main()

Solves the latter issue.

What you may want is:

proc t(x: A | B) =
  echo(if x == nil: "nil" else: $(cast[int](x.impl)))
  
proc t(x: typedesc[A | B]) =
  t(default(x))
# The folllowing can be used for the `nil` case.
t(A)
t(B)

@StefanSalewski
Copy link
Owner Author

Well, should be fixed, try

nimble uninstall gintro
nimble install gintro@#head

StefanSalewski added a commit that referenced this issue Sep 24, 2022
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