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

Converters with openarray #2652

Open
def- opened this issue May 4, 2015 · 4 comments
Open

Converters with openarray #2652

def- opened this issue May 4, 2015 · 4 comments

Comments

@def-
Copy link
Member

def- commented May 4, 2015

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
@megawac
Copy link

megawac commented May 7, 2015

I think "borrowing" methods of another type's implementation or marking a type as compatible (from an API perspective). What I mean by that is that a type marked with a sequence would inherit sequence procedures (.compatible: seq.)? This would essentially be similarish to the toSeq converter without the performance penalty of converting the type to a sequence before iteration

Relevant SO thread

Real purpose: array helpers for https://github.com/megawac/RingBuffer.nim

@Araq
Copy link
Member

Araq commented May 8, 2015

@megawac Reinvented 'concept'?

@megawac
Copy link

megawac commented May 8, 2015

Can concept do this already? I thought it was more similar to distincts
while allowing the user to define additional properties on the type

On Fri, May 8, 2015 at 11:05 AM, Andreas Rumpf notifications@github.com
wrote:

@megawac https://github.com/megawac Reinvented 'concept'?


Reply to this email directly or view it on GitHub
#2652 (comment).

@mratsim
Copy link
Collaborator

mratsim commented Apr 15, 2017

Got hit by this yesterday, here is a test case

Pure seq

import sequtils

proc flatten[T](a: seq[T]): seq[T] = a
proc flatten[T](a: seq[seq[T]]): auto = a.concat.flatten


let s = @[@[1,2,3],@[4,5,6]]
echo s.flatten
# @[1, 2, 3, 4, 5, 6]

Openarray doesn't match seq

import sequtils

proc flatten[T](a: seq[T]): seq[T] = a
proc flatten[T](a: openarray[seq[T]]): auto = a.toSeq.concat.flatten


let s = @[@[1,2,3],@[4,5,6]]
echo s.flatten
# @[@[1, 2, 3], @[4, 5, 6]]

Last case, for some reason there are generic instantiantions now

import sequtils

proc flatten[T](a: openarray[T]): auto = a
proc flatten[T](a: openarray[seq[T]]): auto = a.toSeq.concat.flatten


let s = @[@[1,2,3],@[4,5,6]]
echo s.flatten

Error

openarray_conversion.nim(8, 7) template/generic instantiation from here
openarray_conversion.nim(4, 48) template/generic instantiation from here
lib/pure/collections/sequtils.nim(473, 36) template/generic instantiation from here
lib/system.nim(684, 9) template/generic instantiation from here
lib/system.nim(652, 16) Error: invalid type: 'openarray[seq[int]]' in this context: 'proc (s: var seq[openarray[seq[int]]], len: Natural){.noSideEffect.}'

Please note that while waiting for issue #5708, return type of flatten must be auto

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants