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

Assignment overload does not distinguish between var, let, return values #6348

Closed
mratsim opened this issue Sep 8, 2017 · 3 comments
Closed

Comments

@mratsim
Copy link
Collaborator

mratsim commented Sep 8, 2017

To implement efficient seq-like value semantics, overloading the assignment operator = is needed. However, every use of = will then create a copy even object construction

type
  Baz = object
    data: int
  Foobar = object
    a: int
    data_ref: ref Baz


proc newFoobar(a: int, data: int): Foobar =
  result.a = a
  new result.data_ref
  result.data_ref[].data = data

proc `=`(dest: var Foobar, src: Foobar) =
  dest.a = src.a
  new dest.data_ref
  dest.data_ref[] = src.data_ref[]

  echo "Value copied"

let a = newFoobar(10, 50)
echo a.repr

Output:

Value copied
[a = 10,
data_ref = ref 0x10eec8060 --> [data = 50]]

Trying to implement move optimization doesn't work, I get Error: cannot bind another '=' to: Foobar
with the following code:

type
  Baz = object
    data: int
  Foobar = object
    a: int
    data_ref: ref Baz


proc newFoobar(a: int, data: int): Foobar =
  result.a = a
  new result.data_ref
  result.data_ref[].data = data

proc `=`(dest: var Foobar, src: Foobar) =
  dest.a = src.a
  new dest.data_ref
  dest.data_ref[] = src.data_ref[]

  echo "Value copied"

proc `=`(dest: var Foobar, src: Foobar{call}) =
  system.`=`(dest, src)

  echo "Value moved"

let a = newFoobar(10, 50)
echo a.repr
@edubart
Copy link
Contributor

edubart commented Oct 6, 2017

This makes assignment overloading unusable for huge objects in performance critical apps.

@Araq
Copy link
Member

Araq commented Oct 7, 2017

Assignment overloading is unusable for everything right now.

@Araq
Copy link
Member

Araq commented Sep 24, 2018

I'm closing this since =sink is now a thing and any bugs with it should be reported separately.

@Araq Araq closed this as completed Sep 24, 2018
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

3 participants