-
Notifications
You must be signed in to change notification settings - Fork 122
Description
Pyret has good support for mutation, iteration, etc. This requires being able to have multiple expressions/statements per block. This is not allowed by a well-formedness check, so if I write:
fun sum(l :: List<Number>) -> Number:
var acc = 0
for each(n from l):
acc := acc + n
end
acc
end
I'll get an error prompting me to wrap the body in block:.
I certainly can, as either:
fun sum(l :: List<Number>) -> Number block:
var acc = 0
for each(n from l):
acc := acc + n
end
acc
end
or
fun sum(l :: List<Number>) -> Number:
var acc = 0
block:
for each(n from l):
acc := acc + n
end
acc
end
end
But, it's a bit clumsy, and in the case I want to do multiple things inside the body of the for each loop, I have to add another block:.
fun sum-odd-indices(l :: List<Number>) -> Number block:
var acc = 0
var idx = 0
for each(n from l) block:
when num-modulo(idx,2) == 0:
acc := acc + idx
end
idx := idx + 1
end
acc
end
Now, the reason why this exists is in order to produce an error when someone accidentally puts multiple expressions inside a block (but, not on the same line, as that triggers a different error). Or, intends the function to return two things, not realizing that this is impossible.
My question is -- is the error (which suggests they add block, which in the functional case is not helpful) actually worth it? vs. them having to discover by testing that the final expression in the function is what is returned.