On 12-02-29 01:12 PM, Marijn Haverbeke wrote:
I was actually thinking there wouldn't have to be a special may_return
type at all. The returned value can be an option, and return can store
to that and then break. Code after the call to the iterator then
checks for a `some` value in the return slot and, if found, returns
it.
This is coming into focus a little better. I like where it's going. If
we make these two assumptions though:
- Repurpose the 'for' loop to avoid boilerplate
- Store-into-option to avoid a polymorphic tag
Can we not, again, just get away with 'bool'? That is:
for i in v(z) { ret q; }
becomes:
{
let tmp = none;
v.iter(z) {|i| tmp = some(q); ret false; }
alt tmp {
some(q) { ret q; }
_ { }
}
}
and the iter can be written pleasantly as:
fn iter(self: [int], x: foo, f: fn(int) -> bool) {
let i = 0;
while f(i) { i += 1; }
}
Two questions: is this tolerable, and does it nest properly?
I think it's tolerable, personally. I'd appreciate hearing other
opinions on that.
Meanwhile let's look at nesting...
for i in a {
for j in b {
ret q;
}
}
does not work if the nesting translates, naively, that is, if it becomes:
let tmp1 = none;
a.iter() {|i|
let tmp2 = none;
b.iter() {|j|
tmp2 = some(q); ret false;
}
alt tmp2 {
some(q) { ret q; }
_ {}
}
}
alt tmp {
some(q) { ret q; }
_ {}
}
This is a bad translation -- won't even compile -- since the inner block
winds up with the wrong type. And anyways control flow doesn't propagate
right. I think there's a right translation though. If it instead becomes:
let tmp = none;
a.iter() {|i|
b.iter() {|J|
tmp = some(q); ret false;
}
if tmp != none { ret false; }
}
alt tmp {
some(q) { ret q; }
_ {}
}
Then I think it's ok. IOW there's subtlety to the translation, it has to
be nesting aware. In particular:
- There's only _one_, outermost option to write to. All early rets
write to it.
- Any nested for loop has to inspect the outer option at its end,
and ret false to break its enclosing loop, if the option changed to
'some'.
I _think_ that's enough to make the translation work. Anyone see holes
in it? I'm definitely interested in "getting this right", it's just
somewhat ... subtle.
-Graydon
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev