• entries
359
237
• views
189653

# Day of work: Entry 1

208 views

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.

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?

Anyway problems are being caused by there essentially being a foreach within another foreach?

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...

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.

## Create an account

Register a new account

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.