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

[WIP] seq[T] as regular library code #13324

Closed
wants to merge 20 commits into from

Conversation

timotheecour
Copy link
Member

@timotheecour timotheecour commented Feb 4, 2020

This is an experiment / proof of concept to see whether seq[T] could be turned into a regular library code without special compiler support, which would simplify lots of things in the compiler if that were the case, as well as remove edge cases (eg #13303 (comment) among many others).

many things work but some things don't:

what works

  proc main() =
    var a = @[10,11]
    a.add 12
    doAssert $a == "@[10, 11, 12]"
    doAssert type(a) is seq[int]
    doAssert type(a) is seq
    var a2 = default(type(a))
    for ai in a:
      a2.add ai*10
    doAssert a2 == @[100, 110, 120]
    var a3 = newSeq[int](1)
    doAssert a3 == @[0]
    a3.add 17
    a3.newSeq(4)
    doAssert a3 == @[0, 17, 0, 0]
    var a4 = @["foo"] & @["bar"]
    a4[0] = "FOO"
    a4[1].add "t"
    doAssert a4 == @["FOO", "bart"]
    a4 = @[]
    doAssert a4.len == 0

what doesn't work

  • conversion to openArray from seq
    I tried this but it didn't work:
converter toOpenArray2*[T](a: var seq[T]): var openArray[T] = toOpenArray(a, 0, len(a)-1)

this could be fixed by making OpenArray first class, eg see nim-lang/RFCs#88; or using a compiler patch

  • var s: seq[int] = @[]
    this one is easily fixed, can be fixed using same hack as I did to support:
var s: seq[int]
s = @[] # this already works
  • seq at CT
    const a = @[1,2] gives: system/gc.nim(103, 24) Error: cannot evaluate at compile time: gch

TODO

  • consider what to do with macros.tySeq

links

(EDIT)

  • see tests/destructor/tcustomseqs.nim which looks like it attempted something similar

@cooldome
Copy link
Member

cooldome commented Feb 4, 2020

Problems that you will encounter are const seq and seq in vm.

Possibly, there is generic solution that can make any container that allocates dynamic memory VM and const compatible. Once such solution found and implemented, it will be possible to make seq a library

@cooldome cooldome closed this Feb 4, 2020
@cooldome cooldome reopened this Feb 4, 2020
@krux02
Copy link
Contributor

krux02 commented Feb 5, 2020

Well, there is still this tySeq from macros that you will break for sure. But generally like this Idea.

@Araq
Copy link
Member

Araq commented Apr 20, 2020

Stalled PR, good idea, but const sections are an open problem and the reason why the destructor based new seqs are still builtin for the time being.

@Araq Araq closed this Apr 20, 2020
@timotheecour
Copy link
Member Author

just noticed tests/destructor/tcustomseqs.nim which might've been an attempt at doing the same thing

@timotheecour timotheecour added the stale Staled PR/issues; remove the label after fixing them label Aug 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Staled PR/issues; remove the label after fixing them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants