yield behaves oddly with anonymous functions

I’m implementing a callback based in-game cutscene system. When developing the dialog text portion of the system, I ran into some unexpected behavior when using yield in anonymous functions. I have been unable to figure out why the issue described below is caused. I apologize for the amount of code shown beforehand. Code in Unityscript. showText, Delay and foo all perform a yield return. I can easily work around the issue, but I am quite curious was to why it even occurs.

I would like to know why the following work:

function doDialog(){
    showText("Hi there", function(){
        showText("Hi back");
    });
}

function doDialog(){
    showText("Hi there", function(){
        foo();
        Delay(1, function(){
            showText("Hi back");
        });
}

While the following only make the first showText call:

function doDialog(){
    showText("Hi there", function(){
        Delay(1, function(){
            showText("Hi back");
        });
    });
}

function doDialog(){
    showText("Hi there", function(){
        yield WaitForSeconds(1);
        showText("Hi back");
    });
}

I don’t quite get why the latter methods don’t work. Especially the first, as the only difference between it and the second example in the former set is the inclusion of the call to foo() which is simply foo() { yield; }

Where the test class/script structure is such:

private var isShown : boolean;
private var __text : String;

function Start(){ isShown = false; }

function Update(){
    if(!isShown){
        isShown = true;
        doDialog();
    }
}

function OnGUI(){
    GUI.BeginGroup(new Rect(10,10,200,200));
        GUILayout.Box(__text);
    GUI.EndGroup();
}

function foo(){ yield; }

function Delay(_seconds : float, _func : Function){
    if(_func != null){
        yield WaitForSeconds(_seconds);
        _func();
    }
}

function showText(_text : String){
    showText(_text, null);
}

function showText(_text : String, _func : Function){
    var i : int;
    var _tmp : String = "";

    __text = "";
    for(i = 0; i < _text.Length; i++)
        __text += " ";
    __text += "\r";

    for(i = 0; i < _text.Length; i++){
        while(_text *== " ")*

_tmp += _text[i++];

_tmp += _text*;
__text = _tmp;
for(var j : int = i + 1; j < _text.Length; j++)
__text += " ";
_text += “\r”;
yield WaitForSeconds(0.05);
_
}*

if(_func != null)
_func();
}

Well, after some poking, I believe I can answer my own question. There are a couple things here:

  1. Anonymous functions cannot return an IEnumerator. While it would be awesome, apparently anonymous functions are the second most compiler intensive processes…with IEnumerators being the first. (Or at least very close)
  2. Because Unityscript appears to be interpreted and then compiled, doing too many odd things with anonymous functions and yield in Unityscript appears to confuse the compiler. And so it doesn’t do it’s magical insertion of StartCoroutine.

To get around this? Just went with C#, that way I know what everything is explicitly doing. I had originally wanted to write the cutscene scripts in, well, Unity…script. But, Lambdas look nicer for anonymous functions, C# supports closures, and it works.