last day (14 days later) » 

1:32 PM
2
A: Enumerable and deferred execution of a method

dasblinkenlightOne kind of wrapper that you could use is Lazy<T>: Lazy<IEnumerable<int>> numbers = new Lazy<IEnumerable<int>>(() => GetNumbers()); You can use numbers.Value, or add a wrapper property for it: IEnumerable<int> Numbers { get { return numbers.Value; } } The call of GetNumbers(...

 
Hmm? I cannot use lazy, because I cannot assign IEnumerable x = lazy. I think I need to make a LazyEnumerable wrapper wchi will use Lazy class internally?
 
@zgnilec That's if numbers must be a field. If you could change it to a field+property, you would get an implementation that uses a built-in class that caches the result.
 
You're calling Values as soon as you try to assign the value to the IEnumerable variable that he has, which means you're invoking GetNumbers when you do the assignment (in his case, that's right away) and not when the enumerable is iterated.
 
@Servy No, that's not at all what I do :-) I assign the whole Lazy<T> thingie to a modified variable to avoid calling Value right away.
 
@dasblinkenlight But he already said he can't do that in his situation. He wants to have an IEnumerable that will call the method and return the results when iterated. That's completely doable (and reasonable) and you haven't done it.
 
1:32 PM
@Servy Where did he say that he cannot do that?
 
@Servy That's OP's misunderstanding of my answer - he thought that numbers remains IEnumerable<int>, while I changed it to Lazy<IEnumerable<int>>.
 
No, that's the OP saying that he needs it to be an IEnumerable not a Lazy. Which is of course quite reasonable, he may not even have control over the entire chain of code that uses it. If there is a method/property/etc. that he doesn't control the source of that accepts an IEnumerable, not a Lazy, he needs to match that.
 
@Servy That's your reading of it.
 
@dasblinkenlight The question itself is also very unambigious. He says he wants an IEnumerable that, when iterated, executes the method in question and yields the results. This doesn't do that.
 
1:32 PM
@Servy That's precisely what my property Numbers does, doesn't it?
 
@dasblinkenlight It's not an IEnumerable. And no, it doesn't. It executes the method when Value is called, not when the enumerable is iterated. You have not provided an IEnumerable that defers execution. That's what the question very unambiguously asked for. Now if he asked something vague like, "I'd like some way of deferring execution of GetNumbers until some time in the future" then this could be an option, but he was very specific in his question.
 
@Servy Please re-read the question: "Now GetNumbers() will be called only when I try to access numbers."
 
Please re-read the question: "I want to create IEnumerable which will call GetNumbers() when I will start iterating over that enumerable."
 
@Servy So you agree that the question asks both these things? There goes the "unambiguously asked" part, doesn't it?
 
He unambiguously has stated that he wants to have an IEnumerable<int> as the result. As to what it means to "access" numbers, that particular verb is ambiguous in context. Does it mean resolving the variable to a value, or iterating the sequence? In isolation, that would be ambiguous, but in context it is not; the previous line makes it quite clear that, in context, when he says "access numbers" he means to iterate the sequence. So yes, it's pretty darn unambiguous. You are unambiguously providing a type other than IEnumerable<int> when he unambiguously said that's what he wanted.
 
1:32 PM
@Servy The formatting makes it clear that OP wants to access numbers (the name of the variable), not access numbers (a plural for "number"). It does mean, without a shadow of a reasonable doubt, resolving a variable to a value.
 
No, it's not without a shadow of a doubt. If he said, "Now GetNumbers() will be called only when I try to resolve the numbers variable to a value." then it would mean that without a shadow of a doubt. Of course, if he did mean that then the answer would be it's impossible because there is no way to run the code when the answers variable is resolved to a value, and your answer still doesn't do that. Again, the usage of that one verb is ambiguous in isolation; the context (and the fact that your interpretation is completely impossible) makes the interpretation clear.
 
@Servy My interpretation becomes perfectly possible once you wrap the variable with a property. The implementation becomes pretty darn efficient, too, so the only reason not to use it would be a hard requirement to keep it a variable (which I find hard to imagine in a nicely designed architecture).
 
That still doesn't cause the function to be executed when numbers is evaluated from a variable to a value. It would mean that the function would be executed with the property getter is called. That would be different semantics. And this is of course requiring you to completely ignore all of the places in the question where he explicitly states that he wants to have an IEnumerable specifically, not something else, and that he wants it to execute the method when the enumerable is iterated. So, what, you're just going to pretend that 90% of the question isn't there?
And of course there are any number of reasons why he may need an IEnumerable, and not any other type. As I said before, there could be code he doesn't control that accepts an IEnumerable, he wants to pass one to it representing this data, but defer execution until that other code actually iterates it. It's an entirely reasonable use case that this code in no way handles. And note that doesn't require answers to be a variable, rather it requires the value to be evaluated to be an IEnumerable (which he has stated, explicitly, several times).
 
I agree with @Servy. The above answer will invoke ("fire") the delegate as soon as the reference to the IEnumerable<> is retrieved. But the problem what to not do that. The delegate should be kept even after the IEnumerable<> reference was retrieved. Only when that object is iterated, should the delegate fire. It is perfectly possible that the caller gets the reference early, keeps it, and only iterates it at a later time.
We could write a unit test which the above answer would fail.
 

last day (14 days later) »