Freaky - can't return; out of an IEnumerator

I had some code like this:

beginSomething( 3.141, aCallBack  )

function aCallBack():void
	{
	_aCallBack();
	}

function _aCallBack():IEnumerator
	{
	code
	code
	code
	return;
	}

Bizarrely, perhaps the latest version of Unity, the “return;” at the end makes it throw an exception.

If you delete the return; … no problems.

Does this mean anything to language experts?

Relatedly, I’ve always wondered what is the “correct” default return from :IEnumerator? I tried return null; but no good.

Thanks for any insight …

Keep in mind that you need at least one yield inside of a IEnumerator function or it won’t be a coroutine. Your pseudo code doesn’t show one :wink:


IEnumerator is just an interface, so the function returns an object that implements this interface. The yield statement is just a simple way to create such a class “on the fly” (i don’t mean on the fly at runtime but by the compiler). If you don’t use yield the compiler won’t turn that function into a generator function because it’s a normal function which should return an IEnumerator.

Since i don’t use UnityScript this is the point where it’s not clear what the compiler does since it calls StartCoroutine “automatically” for you. An IEnumerator is just an object, so if you have a reference to such an object you could return it.

For example:

//C#
// this is a generator function
IEnumerator SomeGenerator()
{
    yield return 1;
    yield return 2;
    yield return 4;
}

// NOT a generator since there's no yield.
// That means the function has to return an IEnumerator itself.
IEnumerator SomeNormalFunction()
{
    // Do "normal" stuff
    // calling SomeGenerator will return an object which you could just pass along as return value
    return SomeGenerator();
}

This is how “yield” works in general. Since UnityScript implicitly calls StartCoroutine i’m not sure how the compiler would handle such a case.