The case of Sequence
in Hylo
#1081
Replies: 3 comments 3 replies
-
Some scattered thoughts: Just for posterity: the reason to distinguish There is in general no way to pop elements off the front of a If I had a non-
This obviously screams out for remote parts: then you just have one I don't love that the I'm beginning to think that
I think in some sense that is only true if we don't have remote parts. But then your (@kyouko-taiga this post may have evolved a lot since you read it last). |
Beta Was this translation helpful? Give feedback.
-
(Aside) doesn't |
Beta Was this translation helpful? Give feedback.
-
We discussed the idea that a consuming iterator should be called I am convinced by your arguments that |
Beta Was this translation helpful? Give feedback.
-
Background: Swift's
Sequence
protocolAs I was thinking about the traits required by consuming for loops (see #1080), I wondered about the role of a Swift-like
Sequence
trait in Hylo.Motivation for a
Sequence
-like trait:Often, one may want to iterate over the elements of a container to apply some form of transformation. If that container isn't needed after the iteration, the latter should be able to take ownership of each element. Concrete examples include
map
,filter
, andreduce
:Note that no copy occurs in the above implementation of
map
.Starting from Swift:
It would be pretty simple to model a consuming generator with the following trait, shamelessly copying Swift's design:
Relationship to
Collection
:In Swift,
Collection
is a refinement ofSequence
. We could make the same choice in Hylo but synthesizingSequence
's requirements from a conformance toCollection
would only be possible whenSelf.Element
isCopyable
:The constraint
Self.Element: Copyable
makes it annoying to implementCollection
on arbitrary types. Further,DefaultIterator
is not even a very efficient implementation. It doesn't really satisfies the spirit of the trait, which is supposed to let the client efficiently (i.e., without unnecessary copies) consume the elements of a container in order, one at a time.So I'm not convinced that
Collection
should refineSequence
in Hylo. Instead, I would lean toward having justIterator
and optionally another traitConvertibleToIterator
.Be more generic?
One other way to look at this issue would be to design a kind of maximally generic adapter that can take ownership of a base collection only when necessary. I think it would be interesting to think about what such a type would look like, but it would require quite some work on the design of remote types and polymorphic effects. So I think that is out of scope for a 1.0 version of Hylo.
Beta Was this translation helpful? Give feedback.
All reactions