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

add collect with infered init, refs #16078 fixes #14332 #16089

Merged
merged 11 commits into from
Dec 3, 2020
Merged

add collect with infered init, refs #16078 fixes #14332 #16089

merged 11 commits into from
Dec 3, 2020

Conversation

planetis-m
Copy link
Contributor

@planetis-m planetis-m commented Nov 21, 2020

Supersedes #16082, refs #16078. it is not injected. Reason is, if you write it[i] = a then there is no point using collect, is there? :P

@planetis-m
Copy link
Contributor Author

planetis-m commented Nov 21, 2020

It imports tables and sets, maybe inferring container type should work for seqs only?

of nnkTableConstr:
result[1] = n[0][0]
result[2] = n[0][1]
if bracketExpr.len == 0:
bracketExpr.add(bindSym"initTable")
Copy link
Contributor Author

@planetis-m planetis-m Nov 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idea: use newSeq and produce a seq[(K, V)] Or make it an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, it's not worth it: you can do collect(for i, x in a: (i, x))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that better?

      when (NimMajor, NimMinor) >= (1, 5):
        bracketExpr.add(bindSym"initTable")
      else: error("Table type is not specified")

template adder(res, k, v) = res[k] = v
result[0] = getAst(adder(res, n[0][0], n[0][1]))
of nnkCurly:
result[2] = n[0]
if bracketExpr.len == 0:
bracketExpr.add(bindSym"initHashSet")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, use newSeq and ignore {a}

@planetis-m
Copy link
Contributor Author

For now I just used ident"initTable" and avoided the import.

@planetis-m
Copy link
Contributor Author

planetis-m commented Nov 21, 2020

@moigagoo can you take a look please? For a simple example, I can't reproduce.

import strutils
    let x = collect:
      for d in data.items:
        "bird $1" % [d]
    assert x == @["bird bird", "bird word"]

@moigagoo
Copy link
Contributor

@planetis-m Hi! Sorry, I don't quite follow. Take a look at what exactly? What is it you can't reproduce?

@planetis-m
Copy link
Contributor Author

planetis-m commented Nov 21, 2020

@moigagoo this build failure:

  /home/runner/work/Nim/Nim/pkgstemp/norm/tests/sqlite/trows.nim(16, 7) template/generic instantiation of `suite` from here
  /home/runner/work/Nim/Nim/pkgstemp/norm/tests/sqlite/trows.nim(65, 8) template/generic instantiation of `test` from here
  /home/runner/work/Nim/Nim/pkgstemp/norm/tests/sqlite/trows.nim(70, 10) template/generic instantiation of `with` from here
  /home/runner/work/Nim/Nim/pkgstemp/norm/tests/sqlite/trows.nim(72, 13) template/generic instantiation of `select` from here
  /home/runner/work/Nim/Nim/pkgstemp/norm/src/norm/sqlite.nim(155, 24) template/generic instantiation of `collect` from here
  /home/runner/work/Nim/Nim/pkgstemp/norm/src/norm/sqlite.nim(157, 41) Error: type mismatch: got <>
  but expected one of: 
  proc (s: var seq[string], len: Natural){.noSideEffect.}

@moigagoo
Copy link
Contributor

@planetis-m

So, AFAIU you've modified the behavior of collect macro and this change broke Norm at least in one place where collect is used. Is this correct?

I suggest you try running simple tests like this one with your modified version and see if it runs: https://play.nim-lang.org/#ix=2EYf

@planetis-m
Copy link
Contributor Author

planetis-m commented Nov 21, 2020

no it wasn't modified I added a second macro, it's hidden under a since(1, 5)

    let x = collect(newSeq):
      for d in data.items:
        "bird $1" % [d]
    assert x == @["bird bird", "bird word"]

runs fine.

@moigagoo if you can nimble install looper and import sugar except collect; import looper, then run tests with --expandmacro:collect that would be appreciated.

Or just run with --expandmacro:collect

@moigagoo
Copy link
Contributor

@planetis-m Well I have no idea why it could break ¯_(ツ)_/¯ Try wrapping it in a unittest test macro to replicate the code that is actually being run and that actually fails. Maybe the new version somehow doesn't play well within macros.

A few points:

  1. I haven't read your PR and I don't really plan to. So, I can't see exactly what could go wrong.
  2. Since it's your change to the stdlib, I stand with the belief that it is your responsibility to make it work with existing packages.
  3. Installing a customized version of Nim and running and debugging Norm tests with it is tedious and it really isn't how I plan to spend this beautiful Saturday evening :-)

Sorry for not being helpful, and I really hope I don't sound like an asshole. Just protecting my personal time.

@planetis-m
Copy link
Contributor Author

planetis-m commented Nov 21, 2020

You make good points. However your package is broken, my PR isn't related. Once you get the time, fix it please. Since it's my responsibility for fixing the stlib, its yours to fix your package :)

@moigagoo
Copy link
Contributor

your package is broken, my PR isn't related.

I disagree. The tests pass with a PR that has been pushed after yours: https://github.com/nim-lang/Nim/runs/1435997008?check_suite_focus=true#step:11:519

@planetis-m
Copy link
Contributor Author

Checked the produced ast, PR changed some nimnodes from ident to sym: https://play.nim-lang.org/#ix=2EYD

@planetis-m planetis-m changed the title add collect with infered init, fixes add collect with infered init, refs #16078 fixes #14332 Nov 23, 2020
@planetis-m
Copy link
Contributor Author

planetis-m commented Nov 23, 2020

Can I move collect in a separate module like std/collect? Edit: only if we deprecate sugar.collect

@planetis-m
Copy link
Contributor Author

This can use a review, thank you!

@Araq Araq merged commit 808ab7e into nim-lang:devel Dec 3, 2020
timotheecour pushed a commit to timotheecour/Nim that referenced this pull request Dec 3, 2020
mildred pushed a commit to mildred/Nim that referenced this pull request Jan 11, 2021
ardek66 pushed a commit to ardek66/Nim that referenced this pull request Mar 26, 2021
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

Successfully merging this pull request may close these issues.

3 participants