YarnSpinnerTool / YarnSpinner

Yarn Spinner is a tool for building interactive dialogue in games!
https://yarnspinner.dev
MIT License
2.3k stars 201 forks source link

Compiler incorrectly infers types when nesting functions #328

Open fmoo opened 2 years ago

fmoo commented 2 years ago

What is the current behavior?

When nesting functions, the compiler cannot correctly infer the type of the inner function in order to invoke the outer one.

A yarn error is shown in Unity. The error is also shown in the vscode extension

Please provide the steps to reproduce, and if possible a minimal demo of the problem:

Given the following custom functions:

// using System.Linq;
runner.AddFunction<string>("guid", () => System.Guid.NewGuid().ToString());
runner.AddFunction<string, string>("reverse", (s) => new string(s.Reverse().ToArray()));

Called as such:

<<declare $_spam = "">>
<<set $_spam = reverse(guid())>>

The following error is raised in Unity:

Error compiling: 13:15: Error: reverse parameter 1 expects a undefined, not a undefined
reverse(guid())

The following error is raised in vscode:

Could not find function definition for reverse(YRNMsngCmdDef)
reverse parameter 1 expects a undefined, not a undefined

What is the expected behavior?

There should be no errors shown in unity or vscode for this scenario.

Please tell us about your environment:

Other information

fmoo commented 2 years ago

Also repros on vscode extension v2.2.40

McJones commented 7 months ago

This issue has been a weird one for a while and have made some progress. This comment is more for my own records while this issue is still being worked on.

This isn't being caused by nesting, but is something to do with the registered type inference, but weirder still only when using the AddFunction call on the dialogue runner.

So the following yarn:

<<declare $_spam = "">>
<<set $_spam = reverse("value")>>

does not work if you declare the function via the following code:

runner.AddFunction<string, string>("reverse", (s) => new string(s.Reverse().ToArray()));

but will work fine if you do it like this:

[YarnFunction("reverse")]
public static string Reverse(string value)
{
    return new string(value.Reverse().ToArray());
}

This occurs both on mainline YS/VSCode but also on the WIP 3.0 branch with the improved type system there.