🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Day of work: Entry 1

Published February 07, 2009
Advertisement
After a late awakening, I set into work on Tangent. As a warmup, I added support for the modulo operator (%). Takes two ints, returns the remainder; no big deal. Then came adding an automatic return at the end of yield blocks so the last value wasn't yielded twice. No problem.

I then figured that I better break the take implementation into two parts since there's two things to test. One being using yield methods with parameters, and the second being making phrase-y yield methods. So, the un-phrased test of take:

public static yields<int> take(int amount, IEnumerable<int> fromCollection){	local int ix = 0;	foreach( int rtn in fromCollection ){		if( !(ix < amount) ){			return();		}		yield rtn;		ix = ix + 1;	}}public static yields<int> allInt(){	local int i = 0;	while(true){		yield i;		i = i + 1;	}}public static void main(){	foreach( int x in take(5,allInt()) ){		print x;	}}


As comes as no real surprise, it didn't work.

First problem was an error where the runtime was trying to access Param[1] when it didn't exist. Tracked that down to the take yield method. The anonymous enumerator didn't have amount or fromCollection so the name resolver found a higher level method, which was crap. Turns out the variables I was using to set the members of the anonymous enumerator weren't what I thought they were. Oops.

Second problem was an error where the runtime was looking for .amount and dying. Makes sense, it should've resolved to this.amount. Turns out that the invocation setup for the yieldy method was being dumb and treating it like a static method.

Third problem was an infinite loop. Tracked that down to the inner loop of take. It wasn't exiting the loop, even though the conditional was processed correctly. It seems I was a little incomplete in my updating loops to be yield-block aware. The foreach loop wasn't detecting the yield 'pause' correctly, so just looped again.

And then the code compiled and ran! It spit out: 02468

Doh. I am going to take a break before trying to figure what wildly convoluted stupidity I've managed to cause that sort of error.
Previous Entry Weekend work
0 likes 3 comments

Comments

nerd_boy
Erm, sounds like the yields are (possibly) getting combined somehow. o_O Also, why the deuce are you using two seperate values, rtn and ix? Wouldn't it be better to do away with ix and use rtn for the evaluation?

[Edit]
Anyway problems are being caused by there essentially being a foreach within another foreach?
February 07, 2009 07:44 PM
Telastyn
Well, the conceptual implementation of take is that it returns the first N elements of a list. That the list in the example happens to be the same as the counter is coincidence.

And yes, there's likely some sort of problem due to the yield nesting; likely something where the inner method isn't being restarted like it should. Time to debug...
February 07, 2009 07:52 PM
nerd_boy
Quote: Original post by Telastyn
Well, the conceptual implementation of take is that it returns the first N elements of a list. That the list in the example happens to be the same as the counter is coincidence.

Oh dur. I should pay attention more.
February 07, 2009 08:28 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement