-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
iterable[T] #17196
iterable[T] #17196
Conversation
What exactly is And what exactly does Also, is there any previous discussion of this? |
It's a type class; most other builtin type classes are lowercase.
all calls to an iterator (inline or closure): when true:
template fn(a: iterable) = discard
iterator iota(n: int): auto = (for i in 0..<n: yield i)
fn(iota(3)) # ok
fn(items(@[1,2])) # ok
# fn(@[1,2]) # error by design
I had an RFC in my nim fork (timotheecour#322) |
changelog ?. EDIT(timotheecour): done |
37c0b2b
to
4866f1b
Compare
Apologies, if my questions are rather obvious, I'm trying to understand this:
|
my understanding is these only concerned
this could be done in future work, but is not strictly necessary, and the issues mentioned in PR body can be fixed without such unification, which has its own can of worms. # instead of:
foo(@[1,2])
# use:
foo(items(@[1,2]) but as I showed in this PR, you can have an overload that take iterable and an overload that takes a typed value (possible taking a |
How does this solution compare to the (to me) much more obvious |
iterator iota(n: int): int = (for i in 0..<n: yield i)
assert toSeq(iota(3)) == @[0,1,2] after this PR, this can work with |
Any update on this? I think this would be a very useful addition. If I understand correctly, this should allow us to massively simplify iterator enumerate[T](iter: iterable[T]): (int, T) =
var i = 0
for x in iter:
yield (i, x)
inc i If this would not be possible, could this be achieved in a similar way? |
54a950d
to
a7efee2
Compare
@Araq PTAL; I will update spec in followup PR
I've rebased to fix bitrot conflicts and fixed the case of
this requires #11992 (see tests/magics/tlambda_iterators.nim which has such examples) but would become more typesafe and easier to use with this PR (because of when true:
import std/lambdas
{.push experimental:"alias".}
iterator enumerate(iter: aliassym): (int, char) =
# `auto` or (int, typeof(iter)) will also be possible in future PRs
var i = 0
for x in iter:
yield (i, x)
inc i
for i, ai in enumerate(lambdaIter {'a', 'b', 'c'}):
echo (i, ai) |
Please do it in this PR, I now understand why and how you do it so I could write the spec myself. However, I want to see if my ideas agree with yours. |
What about something like this? template enumerate[T](iter: iterable[T]): iterable[(int, T)] =
(iterator (): (int, T) =
var i = 0
for x in iter:
yield (i, x)
inc i)() I experimented a bit, but couldn't get it to work. Also, when using
EDIT: It seems like the error is unrelated to this PR, also the |
cb0880f
to
09f3fbe
Compare
09f3fbe
to
b152bd4
Compare
done, PTAL @konsumlamm this should be defered to future work, again, see tests/magics/tlambda_iterators.nim from #11992 |
if efWantIterable in flags: | ||
let typ = newTypeS(tyIterable, c) | ||
rawAddSon(typ, result.typ) | ||
result.typ = typ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Problematic. See my final comment in the review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replied in #17196 (comment)
The solution is quite good, however: Imagine a |
macro fn(a: typed) = discard
fn(items(3..6)) produces, both before and after this PR:
Future work can decide whether
what do you mean? if/when we make typed accept iterable, it would turn a CT error into working code |
Thanks, was not aware. That answers my question. |
* fix failing test toSeq in manual which now works * changelog * reject proc fn(a: iterable) * add iterable to spec * remove MCS/UFCS limitation that now works
someIter(args)
is now well typed:iterable[T]
instead ofuntyped
iterable[T]
for iterables, which is more informativetoSeq
,elementType
etc can now be defined withoutuntyped
openArray
iota(3).toSeq
now workssee tests
will fix these
nim-lang/RFCs#512
#9219
#13595
#14148
#16545
and sidestep #14778
and reduce cases where we hit #14827
note
this will make it much easier to implement online algorithms, eg summation functions should be usable in online fashion, more general than
openArray
· Issue nim-lang/Nim#288 · nim-lang/RFCs, avoiding issues related tountyped
, and enabling UFCS/MCSfuture work
typeof(x, asIterable)
which would return iterable[T] instead of T for iterablesor
smarter so that it works with supertypes oftyped
, eg:seq or iterable
myiter is iterable
items
iterator #11167; it seems like a different issuevar info = something().toSeq
now does work; and all templates that can take iterable instead of untyped also do work with MCS/UFCS. But again, untyped params are still problematic for MCS/UFCS and I can't think of a way to fix this because the problem is essentially ambiguous (outside of RFC:untyped(expr)
for untyped params in overloaded routines timotheecour/Nim#630).