Skip to content

proc(x: var X | ref X) fails to compile #4448

@kierdavis

Description

@kierdavis

The following code compiles:

type
  MyObject = object
    x: int

proc doSomething(obj: var MyObject) =
  obj.x = 123

proc doSomething(obj: ref MyObject) =
  obj.x = 123

let refObj = MyObject.new
doSomething(refObj)
echo $refObj.x

var varObj = MyObject(x: 0)
doSomething(varObj)
echo $varObj.x

while the following code does not:

type
  MyObject = object
    x: int

proc doSomething(obj: var MyObject | ref MyObject) =
  obj.x = 123

let refObj = MyObject.new
doSomething(refObj)
echo $refObj.x

var varObj = MyObject(x: 0)
doSomething(varObj)
echo $varObj.x

instead giving the following compilation error:

test.nim(13, 12) template/generic instantiation from here
test.nim(6, 6) Error: 'obj.x' cannot be assigned to

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions