Lazy sequences
What is still open about seq(a) and Iterable: contexts through Iterable, inferring it, and a shorter signature
Sequences, ranges, seq { } and yield are described for users in
Sequences and queries, Ranges,
Workflows and prelude: sequences, and how
they are built, with the design behind them, in Lazy sequences in the Deep dives
section. This page keeps what is still open.
Plan
- The
seqfunctions taking anyIterable(#2005). They take a([c] seq(a)), sinceIterabletakes only values whose sequence needs no contexts: carrying a sequence’s contexts throughtoSeqwould let them take anyIterable(see open questions). - Inferring
Iterablefor a def that writes no type (#2003).def walk = { items -> for items | item -> ... }should be inferred as[Iterable(s)] s -> (). It isn’t, since generalising the def would have to quantifyelem, which its type doesn’t name; a type nothing settles is walked as a list, unless a signature around the loop requiresIterableof it. A written signature pays for it, and how to make that shorter is an open question. - A range of characters waits on a character type (#2196).
Seq.cache, not written yet, would give the sequences that want it stored tails, so that walking one twice does its work once.- Later: fusing chains of
mapandfilterin the compiler, so that a chain doesn’t allocate a closure and a#nextper element at each step.
Open questions
-
A shorter signature.
[Iterable(s)] s -> ()says the right thing, butseq(a) -> ()was easier to read. Two sources of inspiration:- Rust’s
impl Traitin argument position: a context named where a type is expected, with every parameter but its first, stands for a new type variable that the context constrains.def walk: Iterable -> ()would mean[Iterable(s)] s -> (). Each occurrence is its own variable, it is allowed only for parameters, and it works for any context (def describe: ToString -> string). A context’s name where a type goes is NYX0247 (#1447), so this would give that position a meaning rather than need a rule to tell the names apart. - F#‘s flexible types,
#seq<'a>, which mark a parameter as taking the named type or anything that can stand in for it.
Making a plain
seq(a)parameter quietly mean “anything iterable” is ruled out: the annotation would no longer mean what it says. - Rust’s
-
Iterable values that need contexts.
Iterabletakes only values whose sequence needs no contexts, so theseqfunctions take a sequence. Whether and how to carry a sequence’s contexts throughtoSeqis #2005. -
Walking an unbounded range. The compiler rejects a
forwhose only inputs are ranges with no end (NYX0217). But an infiniteforthat ends with areturnis as legitimate as awhileloop that ends the same way, so this could be a default-linter rule instead, as forwhile true.