-
Notifications
You must be signed in to change notification settings - Fork 308
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
Rust Interop - Emitting type decorators in a target language. #2887
Comments
@alexswan10k What if we use @alfonsogarciacaro What is your opinion on emitting custom types? |
@ncave, good idea. I think that might work. this prevents needing to define new attributes and pull them through the AST. Semantically I think this makes sense too because you are declaring what should be emitted for a declared type. |
@alexswan10k This is a good idea! I think it can work without many code changes (probably even without any change in the AST). I like it that it consistent with the general behavior of Actually, it's already possible to emit code for constructors, you only need to place the attribute between the type name and the constructor arguments. So as @ncave says we probably don't need a new attribute, just implement the behavior for I can draft a PR for this using Dart so you can easily port it to Rust. |
That sounds great @alfonsogarciacaro. I am happy to roll this out to the rust version although a working example would definitely help. I Imagine the entity would need to know about the emit attribute values? I guess this could even be done as language specific as you can extend the entity registration in the bespoke ICompiler implementation to carry extra data, or register as a complimentary dictionary or something. Ultimately it needs to be available when we do a type transform. On the topic of imports i am still torn if we even need it in rust for bindings. All external crates can be directly accessed by their fully qualified namespace, so technically you do not need to import anything as long as the crate is registered in cargo.toml. That being said, this could remove noise, and importing traits can expand the available api surface of any type. Maybe this is a separate discussion though as trait representation in bindings is still unclear to me. |
@alexswan10k I was thinking to add the emit case to But implementing Emit for types in the last step (Fable2Dart here) was not difficult, please see #2899. Only a few notes:
|
This is great. Many thanks @alfonsogarciacaro, I will see if I can get this working with Rust. |
I think we have a solution for this going forward. Any objections if I close? |
Following on from #2875, I wanted to discuss possible approaches to creating bindings for languages specifically which need type information. The approach will probably be relevant for any typed target language where there is asymmetry between source and target types, otherwise import would probably suffice.
So currently you can do simple operations by using emit to bridge the impedance mismatch between the F# domain and the underlying Rust domain. Import alone is not sufficient because you effecrively have to transform the parameters and return types as they are called.
This lets you use Vec as if it was a struct with static methods (which is basically how Rust models instances).
This is fine for simple cases, but Rust does not have the level of type inference that F# does, and at some point, you are probably going to want to pass a Vec (or anything you are abstracting) over a boundary (function parameter or add to record etc), which will require type annotation. Currently, this will break because there is no way to define how the type is represented in the target type system. It will output the dotnet type, which is nonsense.
Now notice this is a particularly problematic example because not only is there no definition, but there is also no way to define generic parameters.
Currently, Emit does not work on instances I believe, but presumably, it could be extended to emit on constructor calls, and any instances that need type decorators could perhaps use a new attribute EmitType to instruct Fable on how to generate the type decorator
My generated code might then look like this
In the same way that Emit allows placeholders to exist for parameter variables, perhaps EmitType would be able to do the same for generic parameters. This would allow modelling of generic collections, among other things.
I imagine this is going to require changes to the core Fable AST as the Emit concept is baked right in. Is this feasible?
One final caveat. In rust a constructor is rrally just a function called new. This means it does not have to return the same type, it might be wrapped in a result, or there might be multiple constructors. With all this in mind, perhaps static method are the way to go. The type decorator problem remains though!
Previous discussions covering dart etc #2779
#2774 (comment)
The text was updated successfully, but these errors were encountered: