Skip to content

Improve generic type parsing and referencing#178

Merged
davideicardi merged 4 commits intodynamicexpresso:masterfrom
metoule:fix_174
Oct 30, 2021
Merged

Improve generic type parsing and referencing#178
davideicardi merged 4 commits intodynamicexpresso:masterfrom
metoule:fix_174

Conversation

@metoule
Copy link
Copy Markdown
Contributor

@metoule metoule commented Oct 27, 2021

The arity of the generic type is now considered when trying to get the reference type (e.g. Tuple<,,> is now looked up as Tuple`3 during parsing).

That means it's possible to write:

var target = new Interpreter();
target.Reference(typeof(Tuple<>));
target.Reference(typeof(Tuple<,>));
target.Reference(typeof(Tuple<,,>));
Assert.AreEqual(1, target.Eval("new Tuple<int>(1).Item1"));
Assert.AreEqual("My str item", target.Eval("new Tuple<int, string>(5, \"My str item\").Item2"));
Assert.AreEqual(3, target.Eval("new Tuple<int, int, int>(1, 2, 3).Item3"));

The old way is still working, but in that case you need to add the arity yourself:

var target = new Interpreter();
target.Reference(typeof(Tuple<>), "Toto`1");
target.Reference(typeof(Tuple<,>), "Toto`2");
target.Reference(typeof(Tuple<,,>), "Toto`3");
Assert.AreEqual(1, target.Eval("new Toto<int>(1).Item1"));
Assert.AreEqual("My str item", target.Eval("new Toto<int, string>(5, \"My str item\").Item2"));
Assert.AreEqual(3, target.Eval("new Toto<int, int, int>(1, 2, 3).Item3"));

I took this opportunity to also centralize the way types are parsed, so that it's consistent everywhere.

Fixes #174

Copy link
Copy Markdown
Member

@davideicardi davideicardi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Just one question on the resx ...

Comment thread src/DynamicExpresso.Core/Resources/ErrorMessages.resx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow support for generic types with multiple arity

2 participants