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

Combining ref types with generics causes a compilation error #19

Open
zah opened this issue Jun 19, 2020 · 1 comment
Open

Combining ref types with generics causes a compilation error #19

zah opened this issue Jun 19, 2020 · 1 comment

Comments

@zah
Copy link
Contributor

zah commented Jun 19, 2020

import json
import json_serialization

type
  RpcResponse*[T] = ref object
    result*: T
    id*: int
    error*: string

when isMainModule:
  echo("Hello, World!")
  let jsn = %* {
    "result": "0x0",
    "id": 0
  }
  echo Json.decode($jsn, RpcResponse[string])

Error:

/usr/local/Cellar/nim/1.2.0/nim/lib/system.nim(850, 11) Error: invalid type: 'T' in this context: 'ref RpcResponse:ObjectType' for var
@zah
Copy link
Contributor Author

zah commented Jun 19, 2020

Possible work-around:

import json
import json_serialization

type
  RpcResponse*[T] = object
    result*: T
    id*: int
    error*: string

  RpcResponseRef*[T] = ref RpcResponse[T]

when isMainModule:
  echo("Hello, World!")
  let jsn = %* {
    "result": "0x0",
    "id": 0
  }
  echo Json.decode($jsn, RpcResponse[string])
  echo Json.decode($jsn, RpcResponseRef[string])[] # Notice that I'm dereferencing the result here, so it can be printed by echo

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

1 participant