Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type parsing to support arrays and generics #173

Merged
merged 8 commits into from
Oct 27, 2021

Conversation

metoule
Copy link
Contributor

@metoule metoule commented Oct 20, 2021

Note that I had to remove the token QuestionDot and QuestionOpenBracket, because it prevented the proper parsing of int?[].

It's now possible to write:

var target = new Interpreter();
target.Reference(typeof(IEnumerable<>), "IEnumerable");
target.Reference(typeof(Dictionary<,>), "Dictionary");

Assert.AreEqual(typeof(IEnumerable<int>), target.Eval("typeof(IEnumerable<int>)"));
Assert.AreEqual(typeof(IEnumerable<IEnumerable<int?[]>>), target.Eval("typeof(IEnumerable<IEnumerable<int?[]>>)"));
Assert.AreEqual(typeof(IEnumerable<>), target.Eval("typeof(IEnumerable<>)"));

Assert.AreEqual(typeof(Dictionary<int, string>[,]), target.Eval("typeof(Dictionary<int,string>[,])"));
Assert.AreEqual(typeof(Dictionary<int, IEnumerable<int[]>>), target.Eval("typeof(Dictionary<int, IEnumerable<int[]>>)"));
Assert.AreEqual(typeof(Dictionary<,>), target.Eval("typeof(Dictionary<,>)"));

Assert.AreEqual(typeof(int[]), target.Eval("typeof(int[])"));
Assert.AreEqual(typeof(int?[]), target.Eval("typeof(int?[])"));
Assert.AreEqual(typeof(int?[,,][][,]), target.Eval("typeof(int?[,,][][,])"));

Fixes #172

@davideicardi
Copy link
Member

Thank you @metoule!

Are you able to fix the conflicts? (I have merged the other PR...)

@metoule
Copy link
Contributor Author

metoule commented Oct 25, 2021

@davideicardi I've fixed the conflicts :)

@Pentiva
Copy link
Contributor

Pentiva commented Oct 27, 2021

As far as I can tell, this implementation does not support types with a variable amount of generic type parameters?, e.g.

var target = new Interpreter();
target.Reference(typeof(Tuple<>), "Tuple");
target.Reference(typeof(Tuple<,>), "Tuple");
target.Reference(typeof(Tuple<,,>), "Tuple");

This can be worked-around by naming them Tuple1, Tuple2, etc.
This might need too much to be refactored to be done in this pull request?

@metoule
Copy link
Contributor Author

metoule commented Oct 27, 2021

@Pentiva that's a different issue: the goal of this PR is simply to support parsing generic types in a typeof expression.

Your issue is that it's not possible to reference multiple arity of the same generic type name. That's because under the hood there's a dictionary that maps a type name to the actual type, and therefore the last call to Reference overrides the previous one. That needs to be modified, but it seems out of scope of this PR.

Do you mind creating a new issue?

@Pentiva
Copy link
Contributor

Pentiva commented Oct 27, 2021

@metoule I expected as much, just thought I would mention it in this pull request as it is sub-feature of generics and, until this is merged, falls under the adding generics issue. I'll create a new issue.

Copy link
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!

@davideicardi davideicardi merged commit 7e172a1 into dynamicexpresso:master Oct 27, 2021
@metoule metoule deleted the fix_172 branch July 23, 2022 08:16
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.

Failed to parse typeof(string[])
3 participants