Why does my stream 'head' fail when I give it a Date variable instead of an Integer or a function?
← Streams and Pipelines · Ref: Q1345
The count for a stream limiting stage (head, tail, skip) must be either an Integer value OR a zero-argument function/function-delegate. If you pass a plain variable that is neither an Integer nor a function (for example a Date), the compiler raises E07870 because it cannot derive an item count from that value.
VALID COUNTS
- Integer literal: cat items | head 2 > stdout
- Integer variable: cat items | head limit > stdout
- Function returning Integer: cat items | head howMany > stdout
Note a related but distinct check: if you DO pass a function but its return type is not Integer, you get E07550 (must return Integer) instead. E07870 is specifically the 'this is neither an Integer nor a function' case.
See Q812 for the full catalogue of stream pipeline errors.
Example
defines module qa.streams.headcountfunction defines function //Valid count supplier: zero arguments, returns Integer. howMany() <- rtn as Integer: 2 defines program StreamHeadCountDemo() stdout <- Stdout() items <- ["alpha", "beta", "gamma", "delta"] // === head with an Integer literal === stdout.println("Head 2 (literal):") cat items | head 2 > stdout // === head with a function returning Integer === stdout.println("Head howMany (function returns Integer):") cat items | head howMany > stdout
Common mistakes
E07870 — head/tail/skip need an Integer value or a function/function-delegate. 'howMany' is a zero-argument function returning Integer so it is a valid count supplier, but 'dueDate' is a Date variable, which is neither an Integer nor a function, so the compiler raises E07870. (A function that returns a non-Integer would instead raise E07550.) See ek9 -h E07870 for details.
Incorrect:
cat items | head items > stdout
Correct:
cat items | head howMany > stdout
Other ways to ask this
- What does E07870 (Integer or function required) mean for head/tail/skip?
- Can I pass any variable as the count for head in an EK9 stream?
- Why must the head/tail/skip count be an Integer value or a function?
Coming from another language?
Java: Stream.limit(long) only accepts a primitive long, so there is no delegate form at all. Python: itertools.islice takes a plain int. Rust: .take(usize) takes an integer directly. EK9: head/tail/skip accept either an Integer value or a function/delegate that returns Integer, validated at compile time via E07870.
Keywords: function, skip, count, tail, stream, Integer, E07870, head, delegate