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

Forced to forward declare when overloading = and =sink #13025

Open
mratsim opened this issue Jan 3, 2020 · 0 comments
Open

Forced to forward declare when overloading = and =sink #13025

mratsim opened this issue Jan 3, 2020 · 0 comments

Comments

@mratsim
Copy link
Collaborator

mratsim commented Jan 3, 2020

None of the following compiles

type
  Foo = object
  Bar = object
    foo: Foo

proc `=`*(dest: var Foo, src: Foo) =
  echo "hello World!"
  system.`=`(dest, src)

proc `=sink`*(dst: var Foo, src: Foo) {.inline.} =
  system.`=sink`(dst, src)

proc initFoo(): Foo =
  discard

proc initBar(foo: sink Foo): Bar =
  echo "hi"
  result.foo = foo
  echo "ho"

proc main =
  let x = initFoo()
  echo "ha"
  let y = initBar(x)

main()
Error: cannot bind another '=sink' to: Foo; previous declaration was constructed here implicitly: .../Nim/weave/build/dest.nim(8, 13)
type
  Foo = object
  Bar = object
    foo: Foo

proc `=`*(dest: var Foo, src: Foo) =
  echo "hello World!"
  system.`=`(dest, src)

proc `=sink`*(dst: var Foo, src: Foo) {.inline.} =
  system.`=sink`(dst, src)

proc initFoo(): Foo =
  discard

proc initBar(foo: sink Foo): Bar =
  echo "hi"
  result.foo = foo
  echo "ho"

proc main =
  let x = initFoo()
  echo "ha"
  let y = initBar(x)

main()
Error: cannot bind another '=' to: Foo; previous declaration was constructed here implicitly: .../nim-#devel/lib/system.nim(471, 16)

The workaround is to forward declare one of them.

@mratsim mratsim changed the title Forced to forward declare when overloading = and sink Forced to forward declare when overloading = and =sink Jan 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant