Skip to content

Converters with openarray #2652

Open
Open
@def-

Description

@def-

Converter to seq doesn't get used when the proc expects an openarray:

type
  TestCol*[T] = object
    data*: seq[T]

converter toSeq*[T](x: TestCol[T]): seq[T] = x.data

proc len*(b: TestCol): int = b.data.len

proc `[]`*[T](b: TestCol[T], idx: int): T =
  b.data[idx]

proc `[]=`*[T](b: var TestCol[T], idx: int, item: T) =
  b.data[idx] = item

var x = newSeq[int](3)
var y = TestCol[int](data: x)

y[0] = 1
y[1] = 2
y[2] = 3

#for n in map(toSeq(y), proc (x: int): int = x + 1):
#  echo n

proc f[T](x: seq[T]) = echo x
proc g[T](x: openarray[T]) = echo x

f(y)
g(y) # Error: type mismatch

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions