Open rtulip opened 1 year ago
Now that Haystack has interfaces, we can maybe start thinking about making loops nicer. In rust, a for loop looks like this:
Haystack
rust
let bar = vec![1, 2, 3]; for foo in bar { println!("foo: {foo}") }
Which is really just syntactic sugar (or something close to) for:
let bar = vec![1, 2, 3]; { let iter = bar.into_iter(); while let Some(foo) = iter.next() { println!("foo: {foo}") } }
Going backwards, we could implement similar IntoIter and Iterator interfaces, which would let us write something like this:
IntoIter
Iterator
interface IntoIter<T> { _: Iter fn Iter.into(T) -> [Iter] } interface Iterator<T> { _: Item fn Iter.next(mut T) -> [Opt<Item> bool] } Vec.new::<u64> as [mut bar] // populate bar bar Iter.into as [it] { while *it Iter.next { Opt.unwrap as [foo] "foo: " print foo println } drop }
With some syntactic sugar of our own, we can get a for loop
for
Vec.new::<u64> as [bar] // ... for foo in bar { "foo:" print foo println }
Now that
Haystack
has interfaces, we can maybe start thinking about making loops nicer. Inrust
, a for loop looks like this:Which is really just syntactic sugar (or something close to) for:
Going backwards, we could implement similar
IntoIter
andIterator
interfaces, which would let us write something like this:With some syntactic sugar of our own, we can get a
for
loop