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

new doesn't work on distinct ref type #12427

Closed
krux02 opened this issue Oct 13, 2019 · 3 comments
Closed

new doesn't work on distinct ref type #12427

krux02 opened this issue Oct 13, 2019 · 3 comments

Comments

@krux02
Copy link
Contributor

krux02 commented Oct 13, 2019

distinct hides the ref nature of the type.

Example

import macros

type
  MyObject = ref object
    value: int

  DistinctObject = distinct MyObject

macro debugType(arg: typed) =
  echo "inst: ", arg.getTypeInst.lispRepr
  echo "impl: ", arg.getTypeImpl.lispRepr
  echo "deeper impl: ", arg.getTypeInst[0].getTypeImpl.lispRepr

proc main() =
  let x = new(MyObject)
  let y = new(DistinctObject)
  echo x.value
  debugType(y)
  echo y.value # Error: undeclared field: 'value'

main()

Current Output

(if echo y.value is commented out)

inst: (RefTy (Sym "DistinctObject"))
impl: (RefTy (Sym "DistinctObject"))
deeper impl: (DistinctTy (Sym "MyObject"))
CC: scratch.nim
Hint:  [Link]
Hint: operation successful (23668 lines compiled; 0.315 sec total; 66.59MiB peakmem; Debug Build) [SuccessX]
Hint: /tmp/scratch  [Exec]
0

Expected Output

new on the distinct ref should not make the type into a double indirection.

Additional Information/Possible Solution

I was working on https://github.com/nim-lang/Nim/issues/9566 and the problem is that in options.nim the expression when T is SomePointer does not work for distinct ref types. Then I saw a very similar pattern in system.nim: when (t is ref): in proc new. Usually I would fix this problem with a macro that transforms the type in the way I want to. But in #9566 I could not fix in, because a macro in a type body did not work as I needed it to work and in system.nim macros are not allowed. So I guess a macro is not a solution here.

@cooldome
Copy link
Member

Isn't it by design of distinct types? distinct string is not a string, distinct ref is not a ref.

@Araq
Copy link
Member

Araq commented Oct 15, 2019

Works as designed. But the special casing in system.new is terrible!

@Araq Araq closed this as completed Oct 15, 2019
@krux02
Copy link
Contributor Author

krux02 commented Oct 15, 2019

@Araq, can you then also close #9566, because it seems to be a non bug as well just an incorrect use of distinct.

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

3 participants