If I have a programming language with first class functions. What should the semantics be when a generator function is shared?
For example:
var f = function() {
foreach (i in 0..42)
yield i;
}
int a = f(); // 0
int b = f(); // 1
// Assigning the generator function
var g = f;
int c = g(); // ??
int d = f(); // ??
I can imagine three things:
- c == 2, d == 3 meaning that the generator function is shared
- c == 0, d == 2 meaning that a new generator function is created, with the values initialized
- c == 2, d == 2 meaning that a new generator function is created by copying the current state of the generator
The best answer in my opinion, would provide the most compelling argument to do one mechanism or another. Often I find that prior art is the most persuasive argument.