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

Transformers API issues when porting from Kolasu #12

Open
gabriele-tomassetti opened this issue Sep 20, 2023 · 3 comments
Open

Transformers API issues when porting from Kolasu #12

gabriele-tomassetti opened this issue Sep 20, 2023 · 3 comments

Comments

@gabriele-tomassetti
Copy link
Member

The original Kolasu API for transformers is well suited for Kotlin, but it is not a perfect fit for C#. The two languages have strong similarities, but there are some notable differences that impact this feature:

  • Kotlin allows to use wildcards in generics, such as NodeFactory<*,*>. This is not possible to do in C#
  • Kotlin has more in-depth support for reflection, for example you can restrict an argument to a type descriptor for a property. C# instead supports only a standard Type class. For instance you can a variable similar to this:
PropertyType<Node> property

where the variable property would contain information about a property of the class Node. Similarly, you can have type-safe references to properties, similar Node::children.type would be a reference to the metadata about the property children of Node. This is not possible in C#.

  • Generics constraints are also more usable in Kotlin. You can use them everywhere you use generics, while they are more restricted in C#. For example, you will rarely see RandomGeneric<out T>

The combinations of these limitations means that using the transformers feature in C# in a bit less usable (e.g., you need to define all generics arguments, while many can be automatically inferred in Kotlin) and less safe (we need to use methods like GetType().GetProperty("children") to get a reference to a property, and this is not type-safe, so we could easily mistype a property and have the code fail. To compare between using of the two versions, you can compare the different implementation of a test in Kolasu and C#.

This leaves us with the issue if we want to change the API to make it more C#-like (better for C#-exclusive project) or keep it more in sync with Kotlin (easier to learn and use in cross-platform project). This issue is open to allow discussion and gather feedback. So, at this point the Transformers API should be still considered in beta status.

@alessiostalla
Copy link
Member

In general, other languages don't have the same type system and reflection capabilities of Kotlin, so some variance in the transformer APIs is to be expected. The same is true for Pylasu (in particular) and Tylasu. In these cases, we've tried to follow the Kotlin API where reasonable (e.g. keeping the same class and method names, order of parameters, etc.) without trying to emulate 100% of Kolasu at all costs. For example, passing property references is a convenience in Kotlin, but it's only one of the possibilities; in the general case, we can pass a function. So, in another language, it could be more type-safe to pass a lambda that accesses the property directly, if supported in the language, rather than using reflection.

@gabriele-tomassetti
Copy link
Member Author

Thanks for your feedback. I do understand the general approach. I have also seen other implementations. I may have misstated the issue. I am gathering feedback about what everybody thinks is the best approach and what constitute a natural C# approach to the issue, given that I have personally not seen many examples of this kind of features in C#.

@alessiostalla
Copy link
Member

I have no idea about that, my C# experience dates back to 2005 or so... I would use a lambda but maybe there are more idiomatic ways in modern C#

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants