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

Nim lacks ref object constructor #13373

Open
zevv opened this issue Feb 8, 2020 · 1 comment
Open

Nim lacks ref object constructor #13373

zevv opened this issue Feb 8, 2020 · 1 comment

Comments

@zevv
Copy link
Contributor

@zevv zevv commented Feb 8, 2020

Nim lacks a concise ref object constructor. We can do

type Foo = object
  val: int

let f = Foo(val: 13)

but to create a ref Foo I either need to create a separate ref Foo type, or use code like

let f = new Foo
f.val = 13

Leorize suggested the following snippet for this:

proc new[T: ref object | object](o: sink T): auto =                       
  when T is ref:            
    result = o
  else:
    result = new(T)
    result[] = o     

This would allow new to work as a ref object constructor like so:

let f = new Foo(val: 13)

Any feedback on this appreciated, could something like this go in system.nim?

@hlaaftana

This comment has been minimized.

Copy link
Contributor

@hlaaftana hlaaftana commented Feb 8, 2020

type Foo = object
  val: int

let f = (ref Foo)(val: 13)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.