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

sugar.collect doesn't work with string + other bugs #16078

Closed
timotheecour opened this issue Nov 21, 2020 · 4 comments
Closed

sugar.collect doesn't work with string + other bugs #16078

timotheecour opened this issue Nov 21, 2020 · 4 comments

Comments

@timotheecour
Copy link
Member

timotheecour commented Nov 21, 2020

sugar.collect doesn't work with string
It's a special case but an important one.

Example 1

when true:
  import std/sugar
  let s = collect(newStringOfCap(4)):
    for j in 0..<4: char(j)

Current Output

Error: cannot instantiate: 'newStringOfCap[typeof:

Expected Output

works

Example 2

collect doesn't work with when

when true:
  import std/sugar
  let s = collect(newSeqOfCap(4)):
    when true:
      for j in 0..<4: char(j)
    else:
      for j in 0..<3: char(j)
  echo s

Additional Information

@ringabout
Copy link
Member

collect is a bit hacky, it only supports generics constructor now.

import std/sugar

proc newStringI[T](size: int): string =
  newStringOfCap(size)


let s = collect(newStringI(4)):
  for j in 55 ..< 59: char(j)

echo s

@timotheecour timotheecour changed the title sugar.collect doesn't work with string sugar.collect doesn't work with string + other bugs Nov 21, 2020
@timotheecour
Copy link
Member Author

collect is a bit hacky, it only supports generics constructor now.

ya, and it seems hard/impossible to fix in a way that's not ambiguous, given that a symbol can have both generic and non-generic overloads. Hence #16082 which also offers other advantages

@Araq
Copy link
Member

Araq commented Dec 17, 2020

This works fine:

import sugar

proc newString[T: string](len: int): string = system.newString(len)

var st = collect(newString(3)):
  for i in 0..2: "this does not work"

echo st

@Araq Araq closed this as completed Dec 17, 2020
@timotheecour
Copy link
Member Author

timotheecour commented Dec 17, 2020

this should work without having to introduce this overload, this is clunky.

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

Successfully merging a pull request may close this issue.

3 participants